Skip to content

Commit

Permalink
Merge pull request Azure#16 from Azure/daschult/DependencyVersionScripts
Browse files Browse the repository at this point in the history
Add scripts for manipulating ms-rest-js version
  • Loading branch information
Dan Schulte authored May 17, 2018
2 parents bca5e47 + 4829c66 commit 8235d34
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 5 deletions.
100 changes: 100 additions & 0 deletions .scripts/dependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");

/**
* Get the absolute path to the package.json in this repository.
* @returns {string} The absolute path to the package.json.
*/
function getPackageJsonFilePath() {
return path.resolve(__dirname, "../package.json");
}

/**
* 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
* provided, then the package.json file at the root of this repository will be used.
* @returns {{}} The parsed package.json file contents.
*/
function getPackageJson(packageJsonFilePath) {
if (!packageJsonFilePath) {
packageJsonFilePath = getPackageJsonFilePath();
}
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 = path.resolve(__dirname, "..", "..", 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;

/**
* 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, " "));

const npmInstallCommand = `npm install ${dependencyName}`;
console.log(npmInstallCommand);
execSync(npmInstallCommand, {stdio:[0,1,2]});
}
}
exports.updatePackageJsonDependency = updatePackageJsonDependency;

/**
* 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;
7 changes: 7 additions & 0 deletions .scripts/latest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const dependencies = require("./dependencies");

const localDependencies = dependencies.getDependenciesWithClonedRepositories();
for (const localDependency of localDependencies) {
const version = dependencies.getNpmPackageVersion(localDependency, "latest");
dependencies.updatePackageJsonDependency(localDependency, `^${version}`);
}
6 changes: 6 additions & 0 deletions .scripts/local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const dependencies = require("./dependencies");

const localDependencies = dependencies.getDependenciesWithClonedRepositories();
for (const localDependency of localDependencies) {
dependencies.updatePackageJsonDependency(localDependency, `file:../${localDependency}`);
}
10 changes: 10 additions & 0 deletions .scripts/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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}`);
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"types": "./typings/lib/msRestAzure.d.ts",
"license": "MIT",
"dependencies": {
"ms-rest-js": "0.5.143"
"ms-rest-js": "^0.5.143"
},
"devDependencies": {
"@types/mocha": "^2.2.43",
Expand Down Expand Up @@ -64,6 +64,9 @@
"test:unit": "mocha",
"test:tslint": "tslint -p . -c tslint.json --exclude test/**/*.ts",
"prepare": "npm run build",
"publish-preview": "npm test && shx rm -rf dist/test && node ./.scripts/publish"
"publish-preview": "npm test && shx rm -rf dist/test && node ./.scripts/publish",
"local": "node ./.scripts/local.js",
"preview": "node ./.scripts/preview.js",
"latest": "node ./.scripts/latest.js"
}
}

0 comments on commit 8235d34

Please sign in to comment.