From c99db415927075b7f311dd77a793e446fb9f76d0 Mon Sep 17 00:00:00 2001 From: Cody Chan Date: Fri, 27 Sep 2019 12:23:32 +0800 Subject: [PATCH] feat: set engine version from current svrx --- lib/Generator.js | 8 +++++- lib/utils.js | 13 +++++++++ package-lock.json | 59 ++++++++++++++++++++++++++++++++++++--- package.json | 1 + template/_package.json | 6 ++-- template/example/start.js | 14 ++++------ 6 files changed, 85 insertions(+), 16 deletions(-) diff --git a/lib/Generator.js b/lib/Generator.js index c3f0f05..9d856d6 100644 --- a/lib/Generator.js +++ b/lib/Generator.js @@ -6,7 +6,12 @@ const ora = require('ora'); const chalk = require('chalk'); const Editor = require('./Editor'); -const { spawn, getPluginName, getPackageName } = require('./utils'); +const { + spawn, + getPluginName, + getPackageName, + getEngineVersion, +} = require('./utils'); class Generator extends Editor { constructor({ root, questions }) { @@ -32,6 +37,7 @@ class Generator extends Editor { async write() { const answers = { ...this.answers, + engineVersion: await getEngineVersion(), pluginName: getPluginName(this.answers.projectName, this.answers.scopeName), packageName: getPackageName(this.answers.projectName, this.answers.scopeName), }; diff --git a/lib/utils.js b/lib/utils.js index d0dc881..f2dd818 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,5 +1,6 @@ const cp = require('child_process'); const { paramCase } = require('change-case'); +const requireg = require('requireg'); const isSvrxPlugin = (name) => /^svrx-plugin-/.test(name); exports.isSvrxPlugin = isSvrxPlugin; @@ -60,3 +61,15 @@ exports.getAuthor = () => { return 'svrx'; } }; + +exports.getEngineVersion = async () => { + try { + const Manager = requireg('@svrx/cli/lib'); + new Manager(); // eslint-disable-line + const svrx = await Manager.loadSvrx(); + const version = svrx.Svrx.getCurrentVersion(); + return `^${version.split('.')[0]}.0.0`; + } catch (err) { + return '>=0.0.1'; + } +}; diff --git a/package-lock.json b/package-lock.json index 861d854..181fd8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -947,6 +947,11 @@ "integrity": "sha1-JJXduvbrh0q7Dhvp3yLS5aVEMmw=", "dev": true }, + "deep-extend": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", + "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==" + }, "deep-is": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz", @@ -2239,8 +2244,7 @@ "ini": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz", - "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==", - "dev": true + "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==" }, "inquirer": { "version": "7.0.0", @@ -3094,6 +3098,11 @@ "integrity": "sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw==", "dev": true }, + "nested-error-stacks": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/nested-error-stacks/-/nested-error-stacks-2.0.1.tgz", + "integrity": "sha512-SrQrok4CATudVzBS7coSz26QRSmlK9TzzoFbeKfcPBUFPjcQM9Rqvr/DlJkOrwI/0KcgvMub1n1g5Jt9EgRn4A==" + }, "nice-try": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", @@ -3419,8 +3428,7 @@ "path-parse": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", - "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", - "dev": true + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==" }, "path-type": { "version": "2.0.0", @@ -3522,6 +3530,29 @@ "integrity": "sha1-Q2CxfGETatOAeDl/8RQW4Ybc+7g=", "dev": true }, + "rc": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", + "integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==", + "requires": { + "deep-extend": "^0.6.0", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" + }, + "dependencies": { + "minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" + }, + "strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=" + } + } + }, "read-pkg": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-2.0.0.tgz", @@ -3589,6 +3620,26 @@ "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz", "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==" }, + "requireg": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/requireg/-/requireg-0.2.2.tgz", + "integrity": "sha512-nYzyjnFcPNGR3lx9lwPPPnuQxv6JWEZd2Ci0u9opN7N5zUEPIhY/GbL3vMGOr2UXwEg9WwSyV9X9Y/kLFgPsOg==", + "requires": { + "nested-error-stacks": "~2.0.1", + "rc": "~1.2.7", + "resolve": "~1.7.1" + }, + "dependencies": { + "resolve": { + "version": "1.7.1", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.7.1.tgz", + "integrity": "sha512-c7rwLofp8g1U+h1KNyHL/jicrKg1Ek4q+Lr33AL65uZTinUZHe30D5HlyN5V9NW0JX1D5dXQ4jqW5l7Sy/kGfw==", + "requires": { + "path-parse": "^1.0.5" + } + } + } + }, "resolve": { "version": "1.12.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.12.0.tgz", diff --git a/package.json b/package.json index a5f3ce1..9d7418e 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "fs-extra": "^8.1.0", "inquirer": "^7.0.0", "ora": "^3.4.0", + "requireg": "^0.2.2", "yargs": "^14.0.0" } } diff --git a/template/_package.json b/template/_package.json index 23bc8e1..80759d6 100644 --- a/template/_package.json +++ b/template/_package.json @@ -9,7 +9,7 @@ "<%- pluginName %>" ], "engines": { - "svrx": "~0.0" + "svrx": "<%- engineVersion %>" }, "author": "<%- author %>", "scripts": { @@ -25,10 +25,10 @@ "chai": "^4.2.0", "chai-json-schema": "^1.5.1", "eslint": "^6.2.0", - "global-modules": "^2.0.0", "husky": "^3.0.4", "lint-staged": "^9.2.3", - "mocha": "^6.2.0" + "mocha": "^6.2.0", + "requireg": "^0.2.2" }, "husky": { "hooks": { diff --git a/template/example/start.js b/template/example/start.js index 2102cba..c0d6f8f 100644 --- a/template/example/start.js +++ b/template/example/start.js @@ -1,19 +1,17 @@ const path = require('path'); -const { existsSync } = require('fs'); +const reguireg = require('requireg'); -const svrxCLI = path.resolve(require('global-modules'), '@svrx', 'cli', 'lib'); - -if (!existsSync(svrxCLI)) { +let Manager; +try { + Manager = reguireg('@svrx/cli/lib'); +} catch (err) { console.log('Please install svrx-cli by `npm i @svrx/cli -g`'); process.exit(); } -const Manager = require(svrxCLI); - process.chdir(__dirname); -const manager = new Manager(); -manager.loadConfigFile(); +new Manager(); // eslint-disable-line Manager.loadSvrx({}, { root: __dirname, plugins: [{ path: path.resolve('..') }],