8 changed files with 114 additions and 21 deletions
@ -1,13 +1,20 @@ |
|||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||
const path = require('path'); |
const path = require('path'); |
||||
|
const cmds = require('./src/cmd'); |
||||
const program = require('commander'); |
const program = require('commander'); |
||||
|
const getConfig = require('./src/utils/getConfig'); |
||||
const pkg = require(path.join(__dirname, './package.json')); |
const pkg = require(path.join(__dirname, './package.json')); |
||||
|
|
||||
program |
program |
||||
.version(pkg.version) |
.version(pkg.version) |
||||
.option('-d, --dev', '本地在线开发') |
.option('-d, --dev', '本地开发') |
||||
|
.option('-i, --init', '初始化配置文件') |
||||
.parse(process.argv); |
.parse(process.argv); |
||||
|
|
||||
if(program.dev) { |
Object.keys(cmds).forEach((cmd) => { |
||||
require('./src/cmd/dev')(); |
const CmdCtor = cmds[cmd]; |
||||
} |
if(program[cmd]) { |
||||
|
const cmdInst = new CmdCtor(getConfig()); |
||||
|
cmdInst.run(); |
||||
|
} |
||||
|
}); |
||||
|
@ -0,0 +1,17 @@ |
|||||
|
class Base { |
||||
|
|
||||
|
constructor({config}) { |
||||
|
this._config = config; |
||||
|
return this; |
||||
|
} |
||||
|
|
||||
|
get config() { |
||||
|
return this._config; |
||||
|
} |
||||
|
|
||||
|
get projectPath() { |
||||
|
return process.cwd(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
module.exports = Base; |
@ -0,0 +1,7 @@ |
|||||
|
const Dev = require('./dev'); |
||||
|
const Init = require('./init'); |
||||
|
|
||||
|
module.exports = { |
||||
|
dev: Dev, |
||||
|
init: Init |
||||
|
}; |
@ -0,0 +1,27 @@ |
|||||
|
const path = require('path'); |
||||
|
const fs = require('fs-extra'); |
||||
|
const Base = require('../base'); |
||||
|
|
||||
|
const CONFIG_TPL_PATH = path.join(__dirname, './template/imove.config.js.tpl'); |
||||
|
const OUTPUT_FILE_PATH = path.join(process.cwd(), 'imove.config.js'); |
||||
|
|
||||
|
class Init extends Base { |
||||
|
|
||||
|
getProjectName() { |
||||
|
const pkgPath = path.join(this.projectPath, 'package.json'); |
||||
|
const pkgJson = fs.readJSONSync(pkgPath); |
||||
|
return pkgJson.name; |
||||
|
} |
||||
|
|
||||
|
getOutputContent() { |
||||
|
const tplContent = fs.readFileSync(CONFIG_TPL_PATH, 'utf-8'); |
||||
|
return tplContent.replace('{projectName}', `'${this.getProjectName()}'`); |
||||
|
} |
||||
|
|
||||
|
run() { |
||||
|
fs.outputFileSync(OUTPUT_FILE_PATH, this.getOutputContent()); |
||||
|
console.log(`Create ${OUTPUT_FILE_PATH} at successfully!`); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
module.exports = Init; |
@ -0,0 +1,6 @@ |
|||||
|
const path = require('path'); |
||||
|
|
||||
|
module.exports = { |
||||
|
projectName: {projectName}, |
||||
|
outputPath: path.join(__dirname, './src/logic') |
||||
|
}; |
@ -0,0 +1,23 @@ |
|||||
|
const path = require('path'); |
||||
|
const fs = require('fs'); |
||||
|
|
||||
|
const CONFIG_FILE_NAME = 'imove.config.js'; |
||||
|
const CONFIG_FILE_PATH = path.join(process.cwd(), CONFIG_FILE_NAME); |
||||
|
const DEFAULT_CONFIG = { |
||||
|
outputPath: path.join(process.cwd(), './src/logic/') |
||||
|
}; |
||||
|
const mergeConfig = (config, DEFAULT_CONFIG) => { |
||||
|
// TODO: merge config
|
||||
|
return config; |
||||
|
} |
||||
|
|
||||
|
const getConfig = () => { |
||||
|
const isConfigFileExisted = fs.existsSync(CONFIG_FILE_PATH); |
||||
|
if(isConfigFileExisted) { |
||||
|
return mergeConfig(require(CONFIG_FILE_PATH), DEFAULT_CONFIG); |
||||
|
} else { |
||||
|
return DEFAULT_CONFIG; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
module.exports = getConfig; |
Loading…
Reference in new issue