Skip to content

Commit

Permalink
feat(build): 检测 lock version 文件中是否有官方库,并给出提示
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhaoju committed Aug 29, 2017
1 parent 7b0922c commit e3b3c3d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 41 deletions.
57 changes: 37 additions & 20 deletions lib/commands/build.js
Expand Up @@ -55,9 +55,11 @@ exports.npmInstall = function () {
}

if (UtilFs.fileExists(sysPath.join(cwd, 'yarn.lock'))) {
checkModuleResolvePath(sysPath.join(cwd, 'yarn.lock'));
currentNpm = ncsEnabled ? 'npm_cache_share' : 'yarn';
log('Installing npm modules with ' + (ncsEnabled ? 'npm_cache_share + ' : '') + 'yarn.');
} else if (UtilFs.fileExists(sysPath.join(cwd, 'npm-shrinkwrap.json'))) {
checkModuleResolvePath(UtilFs.fileExists(sysPath.join(cwd, 'npm-shrinkwrap.json')));
currentNpm = ncsEnabled ? 'npm_cache_share' : 'npm';
log('Installing npm modules with ' + (ncsEnabled ? 'npm_cache_share + ' : '') + 'npm.');
} else {
Expand All @@ -68,14 +70,21 @@ exports.npmInstall = function () {
}

// install
var installCmd = currentNpm + ' install --registry https://repo.corp.qunar.com/artifactory/api/npm/npm-qunar' + (currentNpm === 'npm_cache_share' ? ' -d' : '');
var installParams = '--registry https://repo.corp.qunar.com/artifactory/api/npm/npm-qunar ';
if (currentNpm === 'npm_cache_share') {
installParams += '-d';
} else if (currentNpm === 'yarn') {
installParams += '--non-interactive';
}
var installCmd = currentNpm + ' install ' + installParams;

execute(installCmd);
};

exports.run = function (options) {
var min = options.m || options.min || true;
var min = !(options.m === 'false' || options.min === 'false');

// build process
// display version info
process.stdout && process.stdout.write('node version: ') && execute('node -v');
process.stdout && process.stdout.write('npm version: ') && execute('npm -v');
execute('ykit -v');
Expand All @@ -86,23 +95,6 @@ exports.run = function (options) {
clearGitHooks();
clearNodeModules();
log('Finish building.\n');

function clearGitHooks() {
var gitHooksDir = './.git/hooks/';

if (UtilFs.dirExists(gitHooksDir)) {
fs.readdirSync(gitHooksDir).forEach(function (file) {
var currentPath = path.join(gitHooksDir, file);
fs.writeFileSync(currentPath, '');
});
log('Local git hooks have been cleared.');
}
}

function clearNodeModules() {
shell.rm('-rf', 'node_modules');
log('Local node_modules directory has been cleared.');
}
};

function execute(cmd) {
Expand All @@ -125,4 +117,29 @@ function execute(cmd) {
}

return;
}

function clearGitHooks() {
var gitHooksDir = './.git/hooks/';

if (UtilFs.dirExists(gitHooksDir)) {
fs.readdirSync(gitHooksDir).forEach(function (file) {
var currentPath = path.join(gitHooksDir, file);
fs.writeFileSync(currentPath, '');
});
log('Local git hooks have been cleared.');
}
}

function clearNodeModules() {
shell.rm('-rf', 'node_modules');
log('Local node_modules directory has been cleared.');
}

function checkModuleResolvePath(filePath) {
var lockFileName = path.basename(filePath);
var lockFileContent = fs.readFileSync(filePath, 'utf-8');
var npmjsPathNum = lockFileContent.match(/registry\.npmjs\.org/g).length;
logWarn('According to ' + lockFileName + ', ' + ('there are ' + npmjsPathNum + ' packages installed from official registry') + '(https://registry.npmjs.org/). ' + 'This may slow down the build process.');
logDoc('https://ykit.ymfe.org/docs-npm%20shrinkwrap.html');
}
63 changes: 42 additions & 21 deletions src/commands/build.js
Expand Up @@ -55,9 +55,11 @@ exports.npmInstall = function() {
}

if(UtilFs.fileExists(sysPath.join(cwd, 'yarn.lock'))) {
checkModuleResolvePath(sysPath.join(cwd, 'yarn.lock'));
currentNpm = ncsEnabled ? 'npm_cache_share' : 'yarn';
log(`Installing npm modules with ${ncsEnabled ? 'npm_cache_share + ' : ''}yarn.`);
} else if(UtilFs.fileExists(sysPath.join(cwd, 'npm-shrinkwrap.json'))) {
checkModuleResolvePath(UtilFs.fileExists(sysPath.join(cwd, 'npm-shrinkwrap.json')));
currentNpm = ncsEnabled ? 'npm_cache_share' : 'npm';
log(`Installing npm modules with ${ncsEnabled ? 'npm_cache_share + ' : ''}npm.`);
} else {
Expand All @@ -68,17 +70,23 @@ exports.npmInstall = function() {
}

// install
let installParams = '--registry https://repo.corp.qunar.com/artifactory/api/npm/npm-qunar ';
if(currentNpm === 'npm_cache_share') {
installParams += '-d';
} else if (currentNpm === 'yarn') {
installParams += '--non-interactive';
}
const installCmd = (
`${currentNpm} install --registry https://repo.corp.qunar.com/artifactory/api/npm/npm-qunar`
+ (currentNpm === 'npm_cache_share' ? ' -d' : '')
`${currentNpm} install ${installParams}`
);

execute(installCmd);
};

exports.run = function(options) {
const min = options.m || options.min || true;
const min = !(options.m === 'false' || options.min === 'false');

// build process
// display version info
process.stdout && process.stdout.write('node version: ') && execute('node -v');
process.stdout && process.stdout.write('npm version: ') && execute('npm -v');
execute('ykit -v');
Expand All @@ -89,23 +97,6 @@ exports.run = function(options) {
clearGitHooks();
clearNodeModules();
log('Finish building.\n');

function clearGitHooks() {
const gitHooksDir = './.git/hooks/';

if (UtilFs.dirExists(gitHooksDir)) {
fs.readdirSync(gitHooksDir).forEach(function(file) {
const currentPath = path.join(gitHooksDir, file);
fs.writeFileSync(currentPath, '');
});
log('Local git hooks have been cleared.');
}
}

function clearNodeModules() {
shell.rm('-rf', 'node_modules');
log('Local node_modules directory has been cleared.');
}
};

function execute(cmd) {
Expand All @@ -129,3 +120,33 @@ function execute(cmd) {

return;
}

function clearGitHooks() {
const gitHooksDir = './.git/hooks/';

if (UtilFs.dirExists(gitHooksDir)) {
fs.readdirSync(gitHooksDir).forEach(function(file) {
const currentPath = path.join(gitHooksDir, file);
fs.writeFileSync(currentPath, '');
});
log('Local git hooks have been cleared.');
}
}

function clearNodeModules() {
shell.rm('-rf', 'node_modules');
log('Local node_modules directory has been cleared.');
}

function checkModuleResolvePath(filePath) {
const lockFileName = path.basename(filePath);
const lockFileContent = fs.readFileSync(filePath, 'utf-8');
const npmjsPathNum = lockFileContent.match(/registry\.npmjs\.org/g).length;
logWarn(
`According to ${lockFileName}, `
+ `there are ${npmjsPathNum} packages installed from official registry`
+ '(https://registry.npmjs.org/). '
+ 'This may slow down the build process.'
);
logDoc('https://ykit.ymfe.org/docs-npm%20shrinkwrap.html');
}

0 comments on commit e3b3c3d

Please sign in to comment.