-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update npm run docs to work with linebreaks in story add() function. * Include import documentation for components in no-ssr folder.
- Loading branch information
Showing
3 changed files
with
63 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,57 @@ | ||
let fs = require('fs') | ||
|
||
function getImportStatements (pkgdirectory) { | ||
const importStatements = [] | ||
const pkg = fs.readFileSync(pkgdirectory, 'utf-8') | ||
const packageData = JSON.parse(pkg) | ||
const packageMain = packageData.main | ||
const packageName = packageData.name | ||
const importedFile = fs.readFileSync(pkgdirectory.replace('package.json', packageMain), 'utf-8') | ||
|
||
if (packageMain.indexOf('.js') > -1) { | ||
const reg = new RegExp('export\\s({|default)\\s*([A-Za-z]+)', 'gi') | ||
const batchCurlies = [] | ||
let importDefault = null | ||
|
||
// Match | ||
let lastMatch = reg.exec(importedFile) | ||
while (lastMatch !== null) { | ||
if (lastMatch && lastMatch.length === 3) { | ||
const type = lastMatch[1] | ||
const name = lastMatch[2] | ||
if (type === '{') { | ||
batchCurlies.push(name) | ||
} else { | ||
importDefault = name | ||
} | ||
} | ||
lastMatch = reg.exec(importedFile) | ||
} | ||
// Add to array | ||
if (batchCurlies.length > 0) { | ||
importStatements.push(`import { ${batchCurlies.toString().replace(/,/gi, ', ')} } from '${packageName}'`) | ||
} | ||
if (importDefault !== null && batchCurlies.indexOf(importDefault) < 0) { | ||
importStatements.push(`import ${importDefault} from '${packageName}'`) | ||
} | ||
return getImportStatementFromJS(importedFile, packageName) | ||
} else { | ||
// Assume a .vue file. | ||
const importStatements = [] | ||
const reg = new RegExp('name: \'(.*)\'', 'gi') | ||
let lastMatch = reg.exec(importedFile) | ||
if (lastMatch !== null) { | ||
importStatements.push(`import ${lastMatch[1]} from '${packageName}'`) | ||
} | ||
return importStatements | ||
} | ||
} | ||
|
||
function getImportStatementFromJS (importedFile, packageName) { | ||
const importStatements = [] | ||
const reg = new RegExp('export\\s({|default)\\s*([A-Za-z]+)', 'gi') | ||
const batchCurlies = [] | ||
let importDefault = null | ||
|
||
// Match | ||
let lastMatch = reg.exec(importedFile) | ||
while (lastMatch !== null) { | ||
if (lastMatch && lastMatch.length === 3) { | ||
const type = lastMatch[1] | ||
const name = lastMatch[2] | ||
if (type === '{') { | ||
batchCurlies.push(name) | ||
} else { | ||
importDefault = name | ||
} | ||
} | ||
lastMatch = reg.exec(importedFile) | ||
} | ||
// Add to array | ||
if (batchCurlies.length > 0) { | ||
importStatements.push(`import { ${batchCurlies.toString().replace(/,/gi, ', ')} } from '${packageName}'`) | ||
} | ||
if (importDefault !== null && batchCurlies.indexOf(importDefault) < 0) { | ||
importStatements.push(`import ${importDefault} from '${packageName}'`) | ||
} | ||
return importStatements | ||
} | ||
|
||
module.exports = getImportStatements | ||
module.exports = { | ||
getImportStatements: getImportStatements, | ||
getImportStatementFromJS: getImportStatementFromJS | ||
} |