diff --git a/.scripts/dependencies.js b/.scripts/dependencies.js index cf30aff..8c7c62d 100644 --- a/.scripts/dependencies.js +++ b/.scripts/dependencies.js @@ -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. diff --git a/.scripts/latest.js b/.scripts/latest.js index 6123f1d..d421d3e 100644 --- a/.scripts/latest.js +++ b/.scripts/latest.js @@ -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"); \ No newline at end of file diff --git a/.scripts/local.js b/.scripts/local.js index a32eedf..6b49715 100644 --- a/.scripts/local.js +++ b/.scripts/local.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"); \ No newline at end of file diff --git a/.scripts/preview.js b/.scripts/preview.js index 06062ca..5143909 100644 --- a/.scripts/preview.js +++ b/.scripts/preview.js @@ -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"); \ No newline at end of file diff --git a/package.json b/package.json index 8bfb2a9..65c2401 100644 --- a/package.json +++ b/package.json @@ -69,4 +69,4 @@ "preview": "node ./.scripts/preview.js", "latest": "node ./.scripts/latest.js" } -} +} \ No newline at end of file