Delete node_modules and package-lock.json before installing all dependencies

This commit is contained in:
Dan Schulte 2018-05-17 13:10:33 -07:00
Родитель 704167e278
Коммит 6ca30a186a
5 изменённых файлов: 71 добавлений и 13 удалений

Просмотреть файл

@ -5,11 +5,20 @@ const { execSync } = require("child_process");
/**
* Execute the provided command on the shell synchronously.
* @param {string} command The command to execute.
* @param {string} workingDirectory The working directory to execute the command in.
* @returns {void}
*/
function execute(command) {
console.log(command);
execSync(command, {stdio:[0,1,2]});
function execute(command, workingDirectory) {
console.log(`Running "${command}" in "${workingDirectory}"...`);
execSync(command, {cwd: workingDirectory, stdio:[0,1,2]});
}
/**
* Get the absolute path to this repository's folder path.
* @returns {string} The absolute path to this repository's folder path.
*/
function getThisRepositoryFolderPath() {
return path.resolve(__dirname, "..");
}
/**
@ -91,7 +100,7 @@ function runLocalRepositoryNPMScript(repoName, scriptName) {
const packageJson = getPackageJson(packageJsonFilePath);
const repoScripts = packageJson.scripts;
if (repoScripts && repoScripts[scriptName]) {
execute(`npm run ${scriptName} --prefix ${repoFolderPath}`);
execute(`npm run ${scriptName}`, repoFolderPath);
} else {
console.log(`No script named "${scriptName}" is specified in "${packageJsonFilePath}".`);
}
@ -104,9 +113,11 @@ exports.runLocalRepositoryNPMScript = runLocalRepositoryNPMScript;
* will be run for the changed dependency.
* @param {string} dependencyName The name of the dependency to update.
* @param {string} dependencyVersion The version to update the dependency to.
* @returns {void}
* @returns {boolean} Whether or not the dependency needs to be installed.
*/
function updatePackageJsonDependency(dependencyName, dependencyVersion) {
let dependencyChanged = false;
const packageJsonFilePath = getPackageJsonFilePath();
const packageJson = getPackageJson(packageJsonFilePath);
@ -117,12 +128,35 @@ function updatePackageJsonDependency(dependencyName, dependencyVersion) {
packageJson.dependencies[dependencyName] = dependencyVersion;
fs.writeFileSync(packageJsonFilePath, JSON.stringify(packageJson, undefined, " "));
execute(`npm install ${dependencyName}`);
dependencyChanged = true;
}
return dependencyChanged;
}
exports.updatePackageJsonDependency = updatePackageJsonDependency;
/**
* Run NPM install in this repository
* @returns {void}
*/
function refreshNodeModules() {
if (fs.existsSync("./node_modules")) {
try {
execute(`shx rm ./package-lock.json`, getThisRepositoryFolderPath());
} catch (error) {
}
try {
execute(`shx rm -rf ./node_modules`, getThisRepositoryFolderPath());
} catch (error) {
// This will always throw an exception because we're trying to delete shx, which is currently
// running.
}
}
execute(`npm install`, getThisRepositoryFolderPath());
}
exports.refreshNodeModules = refreshNodeModules;
/**
* Get the npm package version of the package with the provided name at the provided tag.
* @param {string} packageName The name of the package.

Просмотреть файл

@ -2,8 +2,16 @@ const dependencies = require("./dependencies");
const localDependencies = dependencies.getDependenciesWithClonedRepositories();
for (const localDependency of localDependencies) {
const version = dependencies.getNpmPackageVersion(localDependency, "latest");
dependencies.updatePackageJsonDependency(localDependency, `^${version}`);
dependencies.runLocalRepositoryNPMScript(localDependency, "latest");
}
let refreshNodeModules = false;
for (const localDependency of localDependencies) {
const version = dependencies.getNpmPackageVersion(localDependency, "latest");
if (dependencies.updatePackageJsonDependency(localDependency, `^${version}`)) {
refreshNodeModules = true;
}
}
if (refreshNodeModules) {
dependencies.refreshNodeModules();
}
dependencies.updatePackageJsonMain("./dist/lib/msRestAzure.js");

Просмотреть файл

@ -2,7 +2,15 @@ const dependencies = require("./dependencies");
const localDependencies = dependencies.getDependenciesWithClonedRepositories();
for (const localDependency of localDependencies) {
dependencies.updatePackageJsonDependency(localDependency, `file:../${localDependency}`);
dependencies.runLocalRepositoryNPMScript(localDependency, "local");
}
let refreshNodeModules = false;
for (const localDependency of localDependencies) {
if (dependencies.updatePackageJsonDependency(localDependency, `file:../${localDependency}`)) {
refreshNodeModules = true;
}
}
if (refreshNodeModules) {
dependencies.refreshNodeModules();
}
dependencies.updatePackageJsonMain("./lib/msRestAzure.ts");

Просмотреть файл

@ -1,12 +1,20 @@
const dependencies = require("./dependencies");
const localDependencies = dependencies.getDependenciesWithClonedRepositories();
for (const localDependency of localDependencies) {
dependencies.runLocalRepositoryNPMScript(localDependency, "preview");
}
let refreshNodeModules = false;
for (const localDependency of localDependencies) {
let version = dependencies.getNpmPackageVersion(localDependency, "preview");
if (!version) {
version = dependencies.getNpmPackageVersion(localDependency, "latest");
}
dependencies.updatePackageJsonDependency(localDependency, `^${version}`);
dependencies.runLocalRepositoryNPMScript(localDependency, "preview");
if (dependencies.updatePackageJsonDependency(localDependency, `^${version}`)) {
refreshNodeModules = true;
}
}
if (refreshNodeModules) {
dependencies.refreshNodeModules();
}
dependencies.updatePackageJsonMain("./dist/lib/msRestAzure.js");

Просмотреть файл

@ -69,4 +69,4 @@
"preview": "node ./.scripts/preview.js",
"latest": "node ./.scripts/latest.js"
}
}
}