Skip to content

Commit 08dc33d

Browse files
committed
feat: 使业务可以获取当前 ykit 环境
1 parent 17d1139 commit 08dc33d

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

lib/models/Project.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ var Config = require('./Config.js'),
1818

1919
var UtilFs = require('../utils/fs.js');
2020

21+
var ENVS = {
22+
LOCAL: 'local',
23+
DEV: 'dev',
24+
PRD: 'prd'
25+
};
26+
2127
var Project = function () {
2228
function Project(cwd) {
2329
_classCallCheck(this, Project);
@@ -84,8 +90,8 @@ var Project = function () {
8490
middlewares: this.middlewares,
8591
packCallbacks: this.packCallbacks,
8692
eslintConfig: this.eslintConfig,
87-
stylelintConfig: this.stylelintConfig
88-
},
93+
stylelintConfig: this.stylelintConfig,
94+
env: this._getCurrentEnv() },
8995
globalConfigs = Manager.readRC().configs || [];
9096

9197
this.options = options = options || {};
@@ -518,6 +524,19 @@ var Project = function () {
518524
return false;
519525
}
520526
}
527+
}, {
528+
key: '_getCurrentEnv',
529+
value: function _getCurrentEnv() {
530+
if (process.argv[2] === 'pack') {
531+
if (process.argv.indexOf('-m') > -1) {
532+
return ENVS.PRD;
533+
} else {
534+
return ENVS.DEV;
535+
}
536+
}
537+
538+
return ENVS.LOCAL;
539+
}
521540
}]);
522541

523542
return Project;

src/models/Project.js

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ let Config = require('./Config.js'),
1212

1313
let UtilFs = require('../utils/fs.js');
1414

15+
const ENVS = {
16+
LOCAL: 'local',
17+
DEV: 'dev',
18+
PRD: 'prd',
19+
}
20+
1521
class Project {
1622
constructor(cwd) {
1723
this.cwd = cwd;
@@ -50,25 +56,26 @@ class Project {
5056
readConfig(options) {
5157
if (this.check()) {
5258
let userConfig = {
53-
cwd: this.cwd,
54-
_manager: Manager,
55-
setConfig: this.config.setCompiler.bind(this.config), // 兼容旧api
56-
setCompile: this.config.setCompiler.bind(this.config), // 兼容旧api
57-
setCompiler: this.config.setCompiler.bind(this.config),
58-
setExports: this.config.setExports.bind(this.config),
59-
setGroupExports: this.config.setGroupExports.bind(this.config),
60-
setSync: this.config.setSync.bind(this.config),
61-
setCommands: this.setCommands.bind(this),
62-
setEslintConfig: this.setEslintConfig.bind(this),
63-
setStylelintConfig: this.setStylelintConfig.bind(this),
64-
config: this.config.getConfig(),
65-
commands: this.commands,
66-
middlewares: this.middlewares,
67-
packCallbacks: this.packCallbacks,
68-
eslintConfig: this.eslintConfig,
69-
stylelintConfig: this.stylelintConfig
70-
},
71-
globalConfigs = Manager.readRC().configs || [];
59+
cwd: this.cwd,
60+
_manager: Manager,
61+
setConfig: this.config.setCompiler.bind(this.config), // 兼容旧api
62+
setCompile: this.config.setCompiler.bind(this.config), // 兼容旧api
63+
setCompiler: this.config.setCompiler.bind(this.config),
64+
setExports: this.config.setExports.bind(this.config),
65+
setGroupExports: this.config.setGroupExports.bind(this.config),
66+
setSync: this.config.setSync.bind(this.config),
67+
setCommands: this.setCommands.bind(this),
68+
setEslintConfig: this.setEslintConfig.bind(this),
69+
setStylelintConfig: this.setStylelintConfig.bind(this),
70+
config: this.config.getConfig(),
71+
commands: this.commands,
72+
middlewares: this.middlewares,
73+
packCallbacks: this.packCallbacks,
74+
eslintConfig: this.eslintConfig,
75+
stylelintConfig: this.stylelintConfig,
76+
env: this._getCurrentEnv(), // 默认为本地环境
77+
},
78+
globalConfigs = Manager.readRC().configs || [];
7279

7380
this.options = options = options || {};
7481
options.ExtractTextPlugin = ExtractTextPlugin;
@@ -479,6 +486,18 @@ class Project {
479486
return false;
480487
}
481488
}
489+
490+
_getCurrentEnv() {
491+
if(process.argv[2] === 'pack') {
492+
if(process.argv.indexOf('-m') > -1) {
493+
return ENVS.PRD
494+
} else {
495+
return ENVS.DEV
496+
}
497+
}
498+
499+
return ENVS.LOCAL
500+
}
482501
}
483502

484503
module.exports = Project;

0 commit comments

Comments
 (0)