This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
chore(check-node-modules): make check/reinstall node_modules work across platforms #12792
Closed
gkalpak
wants to merge
2
commits into
angular:master
from
gkalpak:chore-fix-install-dependencies-on-windows
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ install: | |
- npm config set loglevel http | ||
- npm install -g [email protected] | ||
# Instal npm dependecies and ensure that npm cache is not stale | ||
- scripts/npm/install-dependencies.sh | ||
- node scripts/npm/check-node-modules.js --reinstall | ||
|
||
before_script: | ||
- mkdir -p $LOGS_DIR | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
// Implementation based on | ||
// https://github.com/angular/angular/blob/3b9c08676a4c921bbfa847802e08566fb601ba7a/tools/npm/check-node-modules.js | ||
'use strict'; | ||
|
||
// Imports | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
// Constants | ||
var NPM_SHRINKWRAP_FILE = 'npm-shrinkwrap.json'; | ||
var NPM_SHRINKWRAP_CACHED_FILE = 'node_modules/npm-shrinkwrap.cached.json'; | ||
var FS_OPTS = {encoding: 'utf-8'}; | ||
var PROJECT_ROOT = path.join(__dirname, '../../'); | ||
|
||
// Variables | ||
var progressIndicatorTimer = null; | ||
|
||
// Run | ||
_main(); | ||
|
||
// Functions - Definitions | ||
function _main() { | ||
var reinstallIfStale = process.argv.indexOf('--reinstall') !== -1; | ||
checkNodeModules(reinstallIfStale); | ||
} | ||
|
||
function cacheNpmShrinkwrapFile() { | ||
var absoluteMarkerFilePath = path.join(PROJECT_ROOT, NPM_SHRINKWRAP_FILE); | ||
var absoluteCachedMarkerFilePath = path.join(PROJECT_ROOT, NPM_SHRINKWRAP_CACHED_FILE); | ||
|
||
startProgressIndicator(' Caching marker file...'); | ||
copyFile(absoluteMarkerFilePath, absoluteCachedMarkerFilePath, onCopied); | ||
|
||
// Helpers | ||
function onCopied(err) { | ||
stopProgressIndicator(); | ||
if (err) logError(err); | ||
} | ||
} | ||
|
||
function checkNodeModules(reinstallIfStale) { | ||
var nodeModulesOk = compareMarkerFiles(); | ||
|
||
if (nodeModulesOk) { | ||
console.log(':-) npm dependencies are looking good!'); | ||
} else { | ||
console.warn(':-( npm dependencies are stale or in an unknown state!'); | ||
|
||
if (reinstallIfStale) { | ||
purgeModules(); | ||
installModules(); | ||
} | ||
} | ||
|
||
return nodeModulesOk; | ||
} | ||
|
||
function compareMarkerFiles() { | ||
var absoluteMarkerFilePath = path.join(PROJECT_ROOT, NPM_SHRINKWRAP_FILE); | ||
var absoluteCachedMarkerFilePath = path.join(PROJECT_ROOT, NPM_SHRINKWRAP_CACHED_FILE); | ||
|
||
if (!fs.existsSync(absoluteCachedMarkerFilePath)) return false; | ||
|
||
var markerContent = fs.readFileSync(absoluteMarkerFilePath, FS_OPTS); | ||
var cachedMarkerContent = fs.readFileSync(absoluteCachedMarkerFilePath, FS_OPTS); | ||
|
||
return markerContent === cachedMarkerContent; | ||
} | ||
|
||
// Implementation based on | ||
// https://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js#answer-21995878 | ||
function copyFile(srcPath, dstPath, callback) { | ||
var callbackCalled = false; | ||
|
||
if (!fs.existsSync(srcPath)) { | ||
done(new Error('Missing source file: ' + srcPath)); | ||
return; | ||
} | ||
|
||
var rs = fs.createReadStream(srcPath); | ||
rs.on('error', done); | ||
|
||
var ws = fs.createWriteStream(dstPath); | ||
ws.on('error', done); | ||
ws.on('finish', done); | ||
|
||
rs.pipe(ws); | ||
|
||
// Helpers | ||
function done(err) { | ||
if (callback && !callbackCalled) { | ||
callbackCalled = true; | ||
callback(err); | ||
} | ||
} | ||
} | ||
|
||
// Custom implementation of `rm -rf` that works consistently across OSes | ||
function deleteDirSync(path) { | ||
if (fs.existsSync(path)) { | ||
fs.readdirSync(path).forEach(deleteDirOrFileSync); | ||
fs.rmdirSync(path); | ||
} | ||
|
||
// Helpers | ||
function deleteDirOrFileSync(subpath) { | ||
var curPath = path + '/' + subpath; | ||
|
||
if (fs.lstatSync(curPath).isDirectory()) { | ||
deleteDirSync(curPath); | ||
} else { | ||
fs.unlinkSync(curPath); | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we care to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the initial discussion, we preferred this. But imo we can see if this works, and then change it back afterwards if it's much slower. |
||
|
||
function installModules() { | ||
startProgressIndicator(' Running `npm install` (this may take a while)...'); | ||
|
||
var exec = require('child_process').exec; | ||
var opts = {cwd: PROJECT_ROOT}; | ||
var proc = exec('npm install', opts, onInstalled); | ||
|
||
// TODO(gkalpak): Decide if we actually want these | ||
// proc.stdout.pipe(process.stdout); | ||
// proc.stderr.pipe(process.stderr); | ||
|
||
// Helpers | ||
function onInstalled(err) { | ||
stopProgressIndicator(); | ||
|
||
if (err) { | ||
logError(err); | ||
return; | ||
} | ||
|
||
cacheNpmShrinkwrapFile(); | ||
} | ||
} | ||
|
||
function purgeModules() { | ||
startProgressIndicator(' Purging \'node_modules\'...'); | ||
|
||
var nodeModulesPath = path.join(PROJECT_ROOT, 'node_modules'); | ||
deleteDirSync(nodeModulesPath); | ||
|
||
stopProgressIndicator(); | ||
} | ||
|
||
function logError(err) { | ||
var separator = new Array(81).join('!'); | ||
|
||
console.error(separator); | ||
console.error('Operation completed with errors:'); | ||
console.error(err); | ||
console.error(separator); | ||
} | ||
|
||
function startProgressIndicator(taskDescription) { | ||
stopProgressIndicator(); | ||
|
||
var stdout = process.stdout; | ||
|
||
stdout.write(taskDescription); | ||
progressIndicatorTimer = setInterval(stdout.write.bind(stdout, '.'), 5000); | ||
} | ||
|
||
function stopProgressIndicator() { | ||
if (progressIndicatorTimer) { | ||
clearInterval(progressIndicatorTimer); | ||
progressIndicatorTimer = null; | ||
|
||
process.stdout.write('\n'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We ought to make this BIGGER and add a message about how to fix it. Something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good idea!