2dfire-cli
Advanced tools
+193
-0
@@ -15,2 +15,3 @@ /* | ||
| const fs = require('fs-extra') | ||
| const glob = require('glob') | ||
| const { shell } = require('execa') | ||
@@ -81,2 +82,3 @@ const { setFireConfig } = require('../utils/fire') | ||
| fs.copySync(join(__src, 'static'), join(__root, 'static')) | ||
| fs.removeSync(join(__src, 'static')) | ||
| } | ||
@@ -92,2 +94,190 @@ } | ||
| } | ||
| function backupProject() { | ||
| const date = new Date() | ||
| const y = date.getFullYear() | ||
| const m = date.getMonth() + 1 | ||
| const d = date.getDate() | ||
| const h = date.getHours() | ||
| const M = date.getMinutes() | ||
| const s = date.getSeconds() | ||
| const projectCopy = `project-${y}${m}${d}${h}${M}${s}` | ||
| fs.copySync(__src, join(__root, projectCopy, 'src')) | ||
| fs.copySync(__pkgJson, join(__root, projectCopy, 'package.json')) | ||
| fs.copySync(__page, join(__root, projectCopy, 'page')) | ||
| fs.copySync(join(__root, 'build.sh'), join(__root, projectCopy, 'build.sh')) | ||
| } | ||
| /* | ||
| 为了兼容老项目中用module.export和require('xx') | ||
| */ | ||
| async function transform() { | ||
| const isNodeModule = name => { | ||
| const alias = ['src', 'base', '.'] | ||
| if (alias.some(i => name.startsWith(i))) { | ||
| return false | ||
| } | ||
| if (name.startsWith('apiConfig')) { | ||
| return true | ||
| } | ||
| return ['', '.js'].some(item => | ||
| fs.pathExistsSync(join(__nodeModules, name + item)) | ||
| ) | ||
| } | ||
| const isAlias = key => { | ||
| return ['base', 'src', 'apiConfig'].some(i => key.startsWith(i)) | ||
| } | ||
| const getAlias = key => { | ||
| if (key.startsWith('src')) { | ||
| return join(__root, key) | ||
| } | ||
| if (key.startsWith('base')) { | ||
| return join(__src, key) | ||
| } | ||
| return null | ||
| } | ||
| const isImage = key => { | ||
| return ['png', 'jpg', 'jpeg', 'gif', 'svg'].some(i => key.endsWith('.' + i)) | ||
| } | ||
| const getRequireKeyVal = w => { | ||
| w = w.trim() | ||
| const name = w.substring(w.indexOf(' '), w.indexOf('=')).trim() | ||
| let key = w.substring(w.indexOf('(') + 1, w.lastIndexOf(')')).trim() | ||
| const at = key.charAt(0) | ||
| if (at === "'" || at === '"') { | ||
| key = key.substring(1, key.length - 1) | ||
| } | ||
| return { key, name } | ||
| } | ||
| const varRequireReg = /(var|let|const)[^=]*=\s*require\(.+?\)(?!.default)/g | ||
| const moduleExportsReg = /module.exports\s*=\s*/g | ||
| const requireReg = /require\(.+?\)(?!.default)/g | ||
| const importReg = /import\s*{[^'"]*}\s*from\s*(('.+?')|(".+?"))/g | ||
| const exportDefaultReg = /export\s*default\s*/g | ||
| let flag = false | ||
| /* | ||
| 所有用require的方式引用nodeModules中的模块的代码转为import方式 | ||
| */ | ||
| const $require2Import = function(res) { | ||
| if (varRequireReg.test(res)) { | ||
| flag = true | ||
| return res.replace(varRequireReg, w => { | ||
| const { name, key } = getRequireKeyVal(w) | ||
| if (isNodeModule(key)) { | ||
| return `import ${name} from '${key}'` | ||
| } | ||
| return '\n' + w | ||
| }) | ||
| } | ||
| return res | ||
| } | ||
| //所有的require转为require().default | ||
| const $require2Default = function(res) { | ||
| if (requireReg.test(res)) { | ||
| flag = true | ||
| return res.replace(requireReg, w => { | ||
| const { key } = getRequireKeyVal(w) | ||
| if (isImage(key)) { | ||
| return w | ||
| } | ||
| return `${w}.default` | ||
| }) | ||
| } | ||
| return res | ||
| } | ||
| /* | ||
| 分析所有的import {xx}是否需要转为require().default | ||
| */ | ||
| const $Import2RequireDefault = function(res, path) { | ||
| if (importReg.test(res)) { | ||
| flag = true | ||
| return res.replace(importReg, w => { | ||
| w = w.trim() | ||
| const name = w.substring(w.indexOf(' '), w.indexOf('from')).trim() | ||
| let cutOff = w.indexOf("'") > 0 ? "'" : '"' | ||
| const key = w | ||
| .substring(w.indexOf(`${cutOff}`) + 1, w.lastIndexOf(`${cutOff}`)) | ||
| .trim() | ||
| if (isNodeModule(key)) { | ||
| return w | ||
| } else { | ||
| try { | ||
| //分析导入的文件中是否存在module.exports= | ||
| let importPath = join(path, '../', key) | ||
| if (isAlias(key)) { | ||
| importPath = getAlias(key) | ||
| } | ||
| if (fs.pathExistsSync(importPath + '.js')) { | ||
| importPath = importPath + '.js' | ||
| } else if (fs.pathExistsSync(join(importPath, 'index.js'))) { | ||
| importPath = join(importPath, 'index.js') | ||
| } | ||
| //获取依赖的内容 | ||
| const info = fs.readFileSync(importPath, 'utf8') | ||
| if (moduleExportsReg.test(info)) { | ||
| return `const ${name} = require('${key}').default` | ||
| } else { | ||
| return w | ||
| } | ||
| } catch (e) { | ||
| console.log('\n', e, 11) | ||
| return w | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| return res | ||
| } | ||
| //所有的module.exports=转为export default | ||
| const $module2ExportDefault = function(res) { | ||
| if (moduleExportsReg.test(res)) { | ||
| flag = true | ||
| return res.replace(moduleExportsReg, () => { | ||
| return 'export default ' | ||
| }) | ||
| } | ||
| return res | ||
| } | ||
| const $exportDefault2Module = function(res, path) { | ||
| if ( | ||
| ['daily.js', 'pre.js', 'publish.js', 'dev.js'].some(i => | ||
| path.endsWith(i) | ||
| ) && | ||
| exportDefaultReg.test(res) | ||
| ) { | ||
| flag = true | ||
| return res.replace(exportDefaultReg, () => { | ||
| return 'module.exports= ' | ||
| }) | ||
| } | ||
| return res | ||
| } | ||
| const srcArr = glob.sync('src/**/*.{js,vue}', { cwd: join(__root) }) | ||
| for (let i = 0, len = srcArr.length; i < len; i++) { | ||
| const path = join(__root, srcArr[i]) | ||
| let res = await fs.readFile(path, 'utf8') | ||
| res = res || '' | ||
| flag = false | ||
| res = $require2Import(res) | ||
| res = $require2Default(res) | ||
| res = $Import2RequireDefault(res, path) | ||
| if (flag) { | ||
| await fs.writeFile(path, res) | ||
| } | ||
| } | ||
| /* | ||
| 将所有的除apiConfig外的module.exports=转为export default | ||
| apiConfig中的转为module.exports= | ||
| */ | ||
| for (let i = 0, len = srcArr.length; i < len; i++) { | ||
| const path = join(__root, srcArr[i]) | ||
| let res = await fs.readFile(path, 'utf8') | ||
| flag = false | ||
| res = $module2ExportDefault(res) | ||
| res = $exportDefault2Module(res, path) | ||
| if (flag) { | ||
| await fs.writeFile(path, res) | ||
| } | ||
| } | ||
| } | ||
| module.exports = async function() { | ||
@@ -97,2 +287,3 @@ const initSpinner = getSpinner('Initializing...') | ||
| initSpinner.start() | ||
| // backupProject() | ||
| if (!fs.pathExistsSync(__pkgJson)) { | ||
@@ -136,3 +327,5 @@ await shell('npm init -f') | ||
| await fs.remove(join(__root, 'check_health')) | ||
| await shell('git add .') | ||
| await install() | ||
| await transform() | ||
| initSpinner.stop() | ||
@@ -139,0 +332,0 @@ success('\n√ Initialize completed!') |
+1
-1
@@ -74,3 +74,3 @@ const fs = require('fs-extra') | ||
| await fs.copy(__src, join(targetSrcPath, pageName)) | ||
| setFireConfig(pageName) | ||
| setFireConfig(pageName, true) | ||
| await shell('git add .') | ||
@@ -77,0 +77,0 @@ await delPageTplDir() |
@@ -9,2 +9,3 @@ .DS_Store | ||
| /release | ||
| /local.config.js | ||
| /local.config.js | ||
| /project-* |
+1
-1
| { | ||
| "name": "2dfire-cli", | ||
| "version": "1.9.6", | ||
| "version": "1.9.7", | ||
| "description": "cli", | ||
@@ -5,0 +5,0 @@ "author": "danfan", |
+17
-16
@@ -16,8 +16,10 @@ const fs = require('fs-extra') | ||
| exports.setFireConfig = function(name) { | ||
| if (exports.isFireProject()) { | ||
| if (!fs.pathExistsSync(__fireConfig)) { | ||
| fs.writeFileSync( | ||
| __fireConfig, | ||
| ` | ||
| exports.setFireConfig = function(name, isFire) { | ||
| if (isFire && !exports.isFireProject()) { | ||
| return false | ||
| } | ||
| if (!fs.pathExistsSync(__fireConfig)) { | ||
| fs.writeFileSync( | ||
| __fireConfig, | ||
| ` | ||
| module.exports = { | ||
@@ -27,13 +29,12 @@ page: ['${name}'] | ||
| `.trim() | ||
| ) | ||
| } else { | ||
| const res = fs.readFileSync(__fireConfig, 'utf8') | ||
| fs.writeFileSync( | ||
| __fireConfig, | ||
| res.replace( | ||
| /page\s*:\s*((\[(.+?)\])|('(.+?)')|("(.+?)"))\s*,?/g, | ||
| `page:['${name}'],` | ||
| ) | ||
| } else { | ||
| const res = fs.readFileSync(__fireConfig, 'utf8') | ||
| fs.writeFileSync( | ||
| __fireConfig, | ||
| res.replace( | ||
| /page\s*:\s*((\[(.+?)\])|('(.+?)')|("(.+?)"))\s*,?/g, | ||
| `page:['${name}'],` | ||
| ) | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
@@ -40,0 +41,0 @@ } |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
44303
15.77%936
25.81%