Remove unused dev script functions

This commit is contained in:
Dan Schulte 2018-05-17 10:08:22 -07:00
Родитель 0aa2b6b323
Коммит d0800f5dd6
4 изменённых файлов: 1 добавлений и 134 удалений

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

@ -1,16 +1,5 @@
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
/**
* Execute the provided command on the shell synchronously.
* @param {string} command The command to execute.
* @returns {void}
*/
function execute(command) {
console.log(command);
execSync(command, {stdio:[0,1,2]});
}
/**
* Get the absolute path to the package.json in this repository.
@ -20,15 +9,6 @@ function getPackageJsonFilePath() {
return path.resolve(__dirname, "../package.json");
}
/**
* Get the absolute path to the local clone of the repository with the provided name.
* @param {string} repoName The name of the repository.
* @returns {string} The absolute path to the local clone of the repository.
*/
function getLocalRepositoryPath(repoName) {
return path.resolve(__dirname, "..", "..", repoName);
}
/**
* Get the package.json file contents parsed as a JSON object.
* @param {string=} packageJsonFilePath The path to the package.json file to read. If this is not
@ -42,87 +22,6 @@ function getPackageJson(packageJsonFilePath) {
return JSON.parse(fs.readFileSync(packageJsonFilePath));
}
/**
* Get the dependencies from the provided dependencies dictionary that have local clones.
* @param {{ [packageName: string]: string }} dependencies A dictionary of package names to package
* versions.
* @param {string[]} clonedRepositoryNames The array to put the names of the local cloned
* repositories into.
* @returns {void}
*/
function getClonedRepositories(dependencies, clonedRepositoryNames) {
if (clonedRepositoryNames && dependencies) {
for (const dependencyName in dependencies) {
if (clonedRepositoryNames.indexOf(dependencyName) === -1) {
const repoFolderPath = getLocalRepositoryPath(dependencyName);
if (fs.existsSync(repoFolderPath)) {
clonedRepositoryNames.push(dependencyName);
}
}
}
}
}
/**
* Get the names of the dependencies of this repository that have local clones.
* @returns {string[]} The names of the dependencies of this repository that have local clones.
*/
function getDependenciesWithClonedRepositories() {
const clonedRepositoryNames = [];
const packageJson = getPackageJson();
getClonedRepositories(packageJson.dependencies, clonedRepositoryNames);
getClonedRepositories(packageJson.devDependencies, clonedRepositoryNames);
return clonedRepositoryNames;
}
exports.getDependenciesWithClonedRepositories = getDependenciesWithClonedRepositories;
/**
* Run a script with the provided name in the local clone of the repository with the provided name.
* @param {string} repoName The name of the repository to run the script in.
* @param {string} scriptName The name of the script to run in the local repository.
* @returns {void}
*/
function runLocalRepositoryNPMScript(repoName, scriptName) {
const repoFolderPath = getLocalRepositoryPath(repoName);
const packageJsonFilePath = path.join(repoFolderPath, "package.json");
const packageJson = getPackageJson(packageJsonFilePath);
const repoScripts = packageJson.scripts;
if (repoScripts && repoScripts[scriptName]) {
execute(`npm run ${scriptName} --prefix ${repoFolderPath}`);
} else {
console.log(`No script named "${scriptName}" is specified in "${packageJsonFilePath}".`);
}
}
exports.runLocalRepositoryNPMScript = runLocalRepositoryNPMScript;
/**
* Update this repository's package.json file's dependency version with the provided name to the
* provided version. If the dependency version in the package.json file changes, then "npm install"
* 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}
*/
function updatePackageJsonDependency(dependencyName, dependencyVersion) {
const packageJsonFilePath = getPackageJsonFilePath();
const packageJson = getPackageJson(packageJsonFilePath);
if (packageJson.dependencies[dependencyName] == dependencyVersion) {
console.log(`"${dependencyName}" is already set to "${dependencyVersion}".`);
} else {
console.log(`Changing "${dependencyName}" to "${dependencyVersion}"`)
packageJson.dependencies[dependencyName] = dependencyVersion;
fs.writeFileSync(packageJsonFilePath, JSON.stringify(packageJson, undefined, " "));
execute(`npm install ${dependencyName}`);
}
}
exports.updatePackageJsonDependency = updatePackageJsonDependency;
/**
* Update the package.json property values for "main".
* @param {string} mainValue The value that will be used for "main".
@ -142,16 +41,4 @@ function updatePackageJsonMain(mainValue) {
fs.writeFileSync(packageJsonFilePath, JSON.stringify(packageJson, undefined, " "));
}
}
exports.updatePackageJsonMain = updatePackageJsonMain;
/**
* Get the npm package version of the package with the provided name at the provided tag.
* @param {string} packageName The name of the package.
* @param {string} tag The tag of the version to retrieve.
* @returns {string?} The version of the provided package at the provided tag.
*/
function getNpmPackageVersion(packageName, tag) {
const npmViewResult = JSON.parse(execSync(`npm view ${packageName} --json`, { stdio: ['pipe', 'pipe', 'ignore'] }));
return npmViewResult['dist-tags'][tag];
}
exports.getNpmPackageVersion = getNpmPackageVersion;
exports.updatePackageJsonMain = updatePackageJsonMain;

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

@ -1,9 +1,3 @@
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");
}
dependencies.updatePackageJsonMain("./dist/lib/msRest.js")

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

@ -1,8 +1,3 @@
const dependencies = require("./dependencies");
const localDependencies = dependencies.getDependenciesWithClonedRepositories();
for (const localDependency of localDependencies) {
dependencies.updatePackageJsonDependency(localDependency, `file:../${localDependency}`);
dependencies.runLocalRepositoryNPMScript(localDependency, "local");
}
dependencies.updatePackageJsonMain("./lib/msRest.ts");

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

@ -1,12 +1,3 @@
const dependencies = require("./dependencies");
const localDependencies = dependencies.getDependenciesWithClonedRepositories();
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");
}
dependencies.updatePackageJsonMain("./dist/lib/msRest.js")