forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(check-node-modules): make check/reinstall node_modules work acr…
…oss platforms The previous implementations (based on shell scripts) threw errors on Windows, because it was not able to `rm -rf` 'node_modules' (due to the 255 character limit in file-paths). This implementation works consistently across platforms and is heavily based on 'https://github.com/angular/angular/blob/3b9c08676a4c921bbfa847802e08566fb601ba7a/tools/npm/check-node-modules.js'. Fixes angular#11143 Closes angular#11353 Closes angular#12792
- Loading branch information
Showing
6 changed files
with
140 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,74 @@ | ||
// 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 PROJECT_ROOT = path.join(__dirname, '../../'); | ||
var NODE_MODULES_DIR = 'node_modules'; | ||
var NPM_SHRINKWRAP_FILE = 'npm-shrinkwrap.json'; | ||
var NPM_SHRINKWRAP_CACHED_FILE = NODE_MODULES_DIR + '/npm-shrinkwrap.cached.json'; | ||
|
||
// Run | ||
_main(); | ||
|
||
// Functions - Definitions | ||
function _main() { | ||
var purgeIfStale = process.argv.indexOf('--purge') !== -1; | ||
|
||
process.chdir(PROJECT_ROOT); | ||
checkNodeModules(purgeIfStale); | ||
} | ||
|
||
function checkNodeModules(purgeIfStale) { | ||
var nodeModulesOk = compareMarkerFiles(NPM_SHRINKWRAP_FILE, NPM_SHRINKWRAP_CACHED_FILE); | ||
|
||
if (nodeModulesOk) { | ||
console.log(':-) npm dependencies are looking good!'); | ||
} else if (purgeIfStale) { | ||
console.log(':-( npm dependencies are stale or in an unknown state!'); | ||
console.log(' Purging \'' + NODE_MODULES_DIR + '\'...'); | ||
deleteDirSync(NODE_MODULES_DIR); | ||
} else { | ||
var separator = new Array(81).join('!'); | ||
|
||
console.warn(separator); | ||
console.warn(':-( npm dependencies are stale or in an unknown state!'); | ||
console.warn('You can rebuild the dependencies by running `npm install`.'); | ||
console.warn(separator); | ||
} | ||
|
||
return nodeModulesOk; | ||
} | ||
|
||
function compareMarkerFiles(markerFilePath, cachedMarkerFilePath) { | ||
if (!fs.existsSync(cachedMarkerFilePath)) return false; | ||
|
||
var opts = {encoding: 'utf-8'}; | ||
var markerContent = fs.readFileSync(markerFilePath, opts); | ||
var cachedMarkerContent = fs.readFileSync(cachedMarkerFilePath, opts); | ||
|
||
return markerContent === cachedMarkerContent; | ||
} | ||
|
||
// 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); | ||
} | ||
} | ||
} |
This file contains 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,60 @@ | ||
'use strict'; | ||
|
||
// Imports | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
// Constants | ||
var PROJECT_ROOT = path.join(__dirname, '../../'); | ||
var NODE_MODULES_DIR = 'node_modules'; | ||
var NPM_SHRINKWRAP_FILE = 'npm-shrinkwrap.json'; | ||
var NPM_SHRINKWRAP_CACHED_FILE = NODE_MODULES_DIR + '/npm-shrinkwrap.cached.json'; | ||
|
||
// Run | ||
_main(); | ||
|
||
// Functions - Definitions | ||
function _main() { | ||
process.chdir(PROJECT_ROOT); | ||
copyFile(NPM_SHRINKWRAP_FILE, NPM_SHRINKWRAP_CACHED_FILE, onCopied); | ||
} | ||
|
||
// 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); | ||
} | ||
} | ||
} | ||
|
||
function onCopied(err) { | ||
if (err) { | ||
var separator = new Array(81).join('!'); | ||
|
||
console.error(separator); | ||
console.error( | ||
'Failed to copy `' + NPM_SHRINKWRAP_FILE + '` to `' + NPM_SHRINKWRAP_CACHED_FILE + '`:'); | ||
console.error(err); | ||
console.error(separator); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.