Browse Source

feat: add init command and refactor code

master
smallstonesk 4 years ago
parent
commit
6bbf60b8cc
  1. 15
      packages/cli/index.js
  2. 2
      packages/cli/package.json
  3. 17
      packages/cli/src/cmd/base/index.js
  4. 38
      packages/cli/src/cmd/dev/index.js
  5. 7
      packages/cli/src/cmd/index.js
  6. 27
      packages/cli/src/cmd/init/index.js
  7. 6
      packages/cli/src/cmd/init/template/imove.config.js.tpl
  8. 23
      packages/cli/src/utils/getConfig.js

15
packages/cli/index.js

@ -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();
}
});

2
packages/cli/package.json

@ -4,7 +4,7 @@
"description": "imove client", "description": "imove client",
"main": "index.js", "main": "index.js",
"bin": { "bin": {
"imove-cli": "index.js" "imove": "index.js"
}, },
"keywords": [ "keywords": [
"imove", "imove",

17
packages/cli/src/cmd/base/index.js

@ -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;

38
packages/cli/src/cmd/dev/index.js

@ -1,41 +1,47 @@
const path = require('path'); const path = require('path');
const fs = require('fs-extra'); const fs = require('fs-extra');
const Base = require('../base');
const mergePkg = require('./mergePkg'); const mergePkg = require('./mergePkg');
const simplifyDSL = require('./simplifyDSL'); const simplifyDSL = require('./simplifyDSL');
const extractCodes = require('./extractCodes'); const extractCodes = require('./extractCodes');
const {createServer} = require('../../utils/server'); const {createServer} = require('../../utils/server');
const PROJECT_ROOT_PATH = process.cwd();
const TPL_PATH = path.join(__dirname, './template'); const TPL_PATH = path.join(__dirname, './template');
const LOGIC_BASE_PATH = path.join(process.cwd(), './src/logic');
const setup = () => { class Dev extends Base {
const app = createServer();
app.post('/api/save', async (req, res) => { async save(req, res) {
const {outputPath} = this.config;
// check outputPath whether exsited
await fs.ensureDir(outputPath);
// check dsl whether existed // check dsl whether existed
if (!req.body || !req.body.dsl) { if (!req.body || !req.body.dsl) {
res.status(500).json({isCompiled: false}).end(); res.status(500).json({isCompiled: false}).end();
return; return;
} }
// compile // compile
try { try {
const {dsl} = req.body; const {dsl} = req.body;
await mergePkg(dsl, PROJECT_ROOT_PATH); await simplifyDSL(dsl, outputPath);
await simplifyDSL(dsl, LOGIC_BASE_PATH); await extractCodes(dsl, outputPath);
await extractCodes(dsl, LOGIC_BASE_PATH); await fs.copy(TPL_PATH, outputPath);
await fs.copy(TPL_PATH, LOGIC_BASE_PATH); await mergePkg(dsl, this.projectPath);
res.status(200).json({isCompiled: true}).end(); res.status(200).json({isCompiled: true}).end();
console.log('compile successfully!'); console.log('compile successfully!');
} catch(err) { } catch(err) {
res.status(500).json({isCompiled: false}).end(); res.status(500).json({isCompiled: false}).end();
console.log('compile failed! the error is:', err.message); console.log('compile failed! the error is:', err.message);
} }
}); }
run() {
const app = createServer();
app.post('/api/save', save);
}
} }
module.exports = function() { module.exports = Dev;
fs.ensureDirSync(LOGIC_BASE_PATH);
setup();
};

7
packages/cli/src/cmd/index.js

@ -0,0 +1,7 @@
const Dev = require('./dev');
const Init = require('./init');
module.exports = {
dev: Dev,
init: Init
};

27
packages/cli/src/cmd/init/index.js

@ -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;

6
packages/cli/src/cmd/init/template/imove.config.js.tpl

@ -0,0 +1,6 @@
const path = require('path');
module.exports = {
projectName: {projectName},
outputPath: path.join(__dirname, './src/logic')
};

23
packages/cli/src/utils/getConfig.js

@ -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…
Cancel
Save