From 06c41addb9d43407863adf7b6b2fb0cb7119b6d6 Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Thu, 3 Jan 2019 09:39:37 -0800 Subject: [PATCH] Did a little more refactoring --- .scripts/checkEverything.ts | 40 +++++------ .scripts/common.ts | 23 +++++- gulpfile.ts | 139 ++++++++++++++++-------------------- package.json | 2 +- 4 files changed, 99 insertions(+), 105 deletions(-) diff --git a/.scripts/checkEverything.ts b/.scripts/checkEverything.ts index ebbb176cfc4..4cb75eb3817 100644 --- a/.scripts/checkEverything.ts +++ b/.scripts/checkEverything.ts @@ -1,24 +1,15 @@ -import { checkEverything, contains, fileExistsSync, getArgument, getChildFolderPaths, getDefaultLogger, getName, gitDiff, GitDiffResult, gitStatus, GitStatusResult, joinPath, Logger, normalize, resolvePath } from "@ts-common/azure-js-dev-tools"; +import { checkEverything, contains, getArgument, getDefaultLogger, gitDiff, GitDiffResult, gitStatus, GitStatusResult, joinPath, Logger, normalize, resolvePath } from "@ts-common/azure-js-dev-tools"; import * as path from "path"; +import { getPackageFolderPaths } from "./common"; const logger: Logger = getDefaultLogger(); - -const headReference: string | undefined = getArgument("head-reference", { environmentVariableName: "headReference" }); -const baseReference: string | undefined = getArgument("base-reference", { environmentVariableName: "baseReference" }); - const changedFiles: string[] = []; -let packBaseReference: string | undefined = baseReference; -if (!packBaseReference) { - packBaseReference = "master"; - logger.logInfo(`No base-reference argument specified on command line or in environment variables. Defaulting to "${packBaseReference}".`); -} - -let packHeadReference: string | undefined = headReference; -if (!packHeadReference) { +let headReference: string | undefined = getArgument("head-reference", { environmentVariableName: "headReference" }); +if (!headReference) { const statusResult: GitStatusResult = gitStatus(); - packHeadReference = statusResult.localBranch!; - logger.logInfo(`No head-reference argument specified on command line or in environment variables. Defaulting to "${packHeadReference}".`); + headReference = statusResult.localBranch!; + logger.logInfo(`No head-reference argument specified on command line or in environment variables. Defaulting to "${headReference}".`); const modifiedFiles: string[] | undefined = statusResult.modifiedFiles; if (modifiedFiles) { @@ -26,11 +17,17 @@ if (!packHeadReference) { } } -if (packBaseReference !== packHeadReference) { - const diffResult: GitDiffResult = gitDiff(packBaseReference, packHeadReference); +let baseReference: string | undefined = getArgument("base-reference", { environmentVariableName: "baseReference" }); +if (!baseReference) { + baseReference = "master"; + logger.logInfo(`No base-reference argument specified on command line or in environment variables. Defaulting to "${baseReference}".`); +} + +if (baseReference !== headReference) { + const diffResult: GitDiffResult = gitDiff(baseReference, headReference); changedFiles.push(...diffResult.filesChanged); if (!changedFiles || changedFiles.length === 0) { - logger.logInfo(`Found no changes between "${packBaseReference}" and "${packHeadReference}".`); + logger.logInfo(`Found no changes between "${baseReference}" and "${headReference}".`); } else { logger.logVerbose(`Found the following changed files`) for (const changedFilePath of changedFiles) { @@ -39,14 +36,9 @@ if (packBaseReference !== packHeadReference) { } } -const folderNamesToIgnore: string[] = ["node_modules"]; const repositoryFolderPath: string = resolvePath(__dirname, ".."); const packagesFolderPath: string = joinPath(repositoryFolderPath, "packages"); -const packageFolderPaths: string[] | undefined = getChildFolderPaths(packagesFolderPath, { - recursive: true, - condition: (folderPath: string) => fileExistsSync(joinPath(folderPath, "package.json")), - folderCondition: (folderPath: string) => !contains(folderNamesToIgnore, getName(folderPath)) -}); +const packageFolderPaths: string[] | undefined = getPackageFolderPaths(packagesFolderPath); let exitCode: number = 0; if (!packageFolderPaths) { diff --git a/.scripts/common.ts b/.scripts/common.ts index e60564d1fcc..32805fea6e1 100644 --- a/.scripts/common.ts +++ b/.scripts/common.ts @@ -8,7 +8,7 @@ import { execSync } from "child_process"; import * as fssync from "fs"; import { promises as fs } from "fs"; import * as path from "path"; -import * as yargs from "yargs"; +import { getChildFolderPaths, fileExistsSync, joinPath, readPackageJsonFileSync, PackageJson, getName } from '@ts-common/azure-js-dev-tools'; export function arrayContains(array: T[], el: T): boolean { return array.indexOf(el) != -1 @@ -90,4 +90,25 @@ export function findAzureRestApiSpecsRepositoryPathSync(): string | undefined { function containsDirectorySync(directoryName: string, parentPath: string): boolean { return fssync.existsSync(path.resolve(parentPath, directoryName)); +} + +function isPackageFolderPath(folderPath: string, packagesToIgnore: string[]): boolean { + let result = false; + const packageJsonFilePath: string = joinPath(folderPath, "package.json"); + if (fileExistsSync(packageJsonFilePath)) { + const packageJson: PackageJson = readPackageJsonFileSync(packageJsonFilePath); + result = !contains(packagesToIgnore, packageJson.name!); + } + return result; +} + +export const packagesToIgnore: string[] = ["@azure/keyvault", "@azure/template"]; +export const folderNamesToIgnore: string[] = ["node_modules"]; + +export function getPackageFolderPaths(packagesFolderPath: string): string[] | undefined { + return getChildFolderPaths(packagesFolderPath, { + recursive: true, + condition: (folderPath: string) => isPackageFolderPath(folderPath, packagesToIgnore), + folderCondition: (folderPath: string) => !contains(folderNamesToIgnore, getName(folderPath)) + }); } \ No newline at end of file diff --git a/gulpfile.ts b/gulpfile.ts index 2bbfad8be5d..e8bf1bf053d 100644 --- a/gulpfile.ts +++ b/gulpfile.ts @@ -10,7 +10,7 @@ import gulp from "gulp"; import * as path from "path"; import PluginError from "plugin-error"; import { Argv, CommandLineOptions, getCommandLineOptions } from "./.scripts/commandLine"; -import { endsWith } from "./.scripts/common"; +import { endsWith, getPackageFolderPaths, packagesToIgnore } from "./.scripts/common"; import { getDataFromPullRequest } from "./.scripts/github"; import { generateAllMissingSdks, generateMissingSdk, generateSdk, generateTsReadme, regenerate } from "./.scripts/gulp"; import { Logger } from "./.scripts/logger"; @@ -151,29 +151,6 @@ gulp.task('codegen', async () => { await generateSdk(argv.azureRestAPISpecsRoot, argv.azureSDKForJSRepoRoot, argv.package, argv.use, argv.debugger); }); -const folderNamesToIgnore: string[] = ["node_modules"]; - -function getAllPackageFolders(folderPath: string, result?: string[]): string[] { - if (result == undefined) { - result = []; - } - - const folderName: string = path.basename(folderPath); - if (folderNamesToIgnore.indexOf(folderName) === -1 && fs.existsSync(folderPath) && fs.lstatSync(folderPath).isDirectory()) { - const packageJsonFilePath: string = path.join(folderPath, "package.json"); - if (fs.existsSync(packageJsonFilePath) && fs.lstatSync(packageJsonFilePath).isFile()) { - result.push(folderPath); - } - - for (const folderEntryName of fs.readdirSync(folderPath)) { - const folderEntryPath: string = path.join(folderPath, folderEntryName); - getAllPackageFolders(folderEntryPath, result); - } - } - - return result; -} - function pack(): void { const runOptions: RunOptions = { log: (text: string) => _logger.logTrace(text), @@ -228,73 +205,77 @@ function pack(): void { } } - const packagesToSkip: string[] = ["@azure/keyvault", "@azure/template"]; - const packageFolderRoot: string = path.resolve(__dirname, "packages"); - _logger.logTrace(`INFO: Searching for package folders in ${packageFolderRoot}`);; - for (const packageFolderPath of getAllPackageFolders(packageFolderRoot)) { - _logger.logTrace(`INFO: Processing ${packageFolderPath}`); - - const npm = new NPMScope({ executionFolderPath: packageFolderPath }); - const packageJsonFilePath: string = joinPath(packageFolderPath, "package.json"); - const packageJson: { [propertyName: string]: any } = require(packageJsonFilePath); - const packageName: string = packageJson.name; - - if (packagesToSkip.indexOf(packageName) !== -1) { - _logger.log(`INFO: Skipping package ${packageName}`); - ++skippedPackages; - } else if (!args.package || args.package === packageName || endsWith(packageName, `-${args.package}`)) { - const localPackageVersion: string = packageJson.version; - if (!localPackageVersion) { - _logger.log(`ERROR: "${packageJsonFilePath}" doesn't have a version specified.`); - errorPackages++; - } - else { - let shouldPack: boolean = false; - - if (toPack === PackagesToPack.All) { - shouldPack = true; - } else if (toPack === PackagesToPack.DifferentVersion) { - let npmPackageVersion: string | undefined; - try { - const npmViewResult: NPMViewResult = npm.view({ packageName, ...runOptions, showCommand: false, showOutput: false }); - const distTags: StringMap | undefined = npmViewResult["dist-tags"]; - npmPackageVersion = distTags && distTags["latest"]; - } - catch (error) { - // This happens if the package doesn't exist in NPM. - } - - _logger.logTrace(`Local version: ${localPackageVersion}, NPM version: ${npmPackageVersion}`); - shouldPack = localPackageVersion !== npmPackageVersion; - } else if (toPack === PackagesToPack.BranchHasChanges) { - const packageFolderPathWithSep: string = normalize(packageFolderPath + path.posix.sep); - shouldPack = !!changedFiles && contains(changedFiles, (changedFilePath: string) => normalize(changedFilePath).startsWith(packageFolderPathWithSep)); + _logger.logTrace(`INFO: Searching for package folders in ${packageFolderRoot}`); + const packageFolderPaths: string[] | undefined = getPackageFolderPaths(packageFolderRoot); + if (!packageFolderPaths) { + _logger.logTrace(`INFO: The folder ${packageFolderPaths} doesn't exist.`); + } else { + for (const packageFolderPath of packageFolderPaths) { + _logger.logTrace(`INFO: Processing ${packageFolderPath}`); + + const npm = new NPMScope({ executionFolderPath: packageFolderPath }); + const packageJsonFilePath: string = joinPath(packageFolderPath, "package.json"); + const packageJson: { [propertyName: string]: any } = require(packageJsonFilePath); + const packageName: string = packageJson.name; + + if (packagesToIgnore.indexOf(packageName) !== -1) { + _logger.log(`INFO: Skipping package ${packageName}`); + ++skippedPackages; + } else if (!args.package || args.package === packageName || endsWith(packageName, `-${args.package}`)) { + const localPackageVersion: string = packageJson.version; + if (!localPackageVersion) { + _logger.log(`ERROR: "${packageJsonFilePath}" doesn't have a version specified.`); + errorPackages++; } - - if (!shouldPack) { - upToDatePackages++; - } else { - _logger.log(`Packing package "${packageName}" with version "${localPackageVersion}"...${args.whatif ? " (SKIPPED)" : ""}`); - if (!args.whatif) { + else { + let shouldPack: boolean = false; + + if (toPack === PackagesToPack.All) { + shouldPack = true; + } else if (toPack === PackagesToPack.DifferentVersion) { + let npmPackageVersion: string | undefined; try { - npm.pack(runOptions); - const packFileName = `${packageName.replace("/", "-").replace("@", "")}-${localPackageVersion}.tgz` - const packFilePath = path.join(packageFolderPath, packFileName); - fs.renameSync(packFilePath, path.join(dropFolderPath, packFileName)); - _logger.log(`Filename: ${packFileName}`); - packedPackages++; + const npmViewResult: NPMViewResult = npm.view({ packageName, ...runOptions, showCommand: false, showOutput: false }); + const distTags: StringMap | undefined = npmViewResult["dist-tags"]; + npmPackageVersion = distTags && distTags["latest"]; } catch (error) { - errorPackages++; + // This happens if the package doesn't exist in NPM. } + + _logger.logTrace(`Local version: ${localPackageVersion}, NPM version: ${npmPackageVersion}`); + shouldPack = localPackageVersion !== npmPackageVersion; + } else if (toPack === PackagesToPack.BranchHasChanges) { + const packageFolderPathWithSep: string = normalize(packageFolderPath + path.posix.sep); + shouldPack = !!changedFiles && contains(changedFiles, (changedFilePath: string) => normalize(changedFilePath).startsWith(packageFolderPathWithSep)); + } + + if (!shouldPack) { + upToDatePackages++; } else { - skippedPackages++; + _logger.log(`Packing package "${packageName}" with version "${localPackageVersion}"...${args.whatif ? " (SKIPPED)" : ""}`); + if (!args.whatif) { + try { + npm.pack(runOptions); + const packFileName = `${packageName.replace("/", "-").replace("@", "")}-${localPackageVersion}.tgz` + const packFilePath = path.join(packageFolderPath, packFileName); + fs.renameSync(packFilePath, path.join(dropFolderPath, packFileName)); + _logger.log(`Filename: ${packFileName}`); + packedPackages++; + } + catch (error) { + errorPackages++; + } + } else { + skippedPackages++; + } } } } } } + function padLeft(value: number, minimumWidth: number, padCharacter: string = " "): string { let result: string = value.toString(); diff --git a/package.json b/package.json index c413ecc87bd..c39c342a36e 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ }, "devDependencies": { "@octokit/rest": "15.17.0", - "@ts-common/azure-js-dev-tools": "file:C:/Users/daschult/Sources/azure-js-dev-tools", + "@ts-common/azure-js-dev-tools": "^0.7.0", "@types/glob": "^7.1.1", "@types/gulp": "^4.0.5", "@types/js-yaml": "^3.11.2",