Reduce duplication between .scripts and gulpfile

This commit is contained in:
Dan Schulte 2019-01-03 09:12:46 -08:00
Родитель abdfdceffd
Коммит fdb5ff97eb
7 изменённых файлов: 60 добавлений и 66 удалений

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

@ -1,19 +1,10 @@
import { checkEverything, contains, fileExistsSync, getChildFolderPaths, getDefaultLogger, getName, gitDiff, GitDiffResult, gitStatus, GitStatusResult, joinPath, Logger, normalize, resolvePath } from "@ts-common/azure-js-dev-tools";
import { checkEverything, contains, fileExistsSync, getArgument, getChildFolderPaths, getDefaultLogger, getName, gitDiff, GitDiffResult, gitStatus, GitStatusResult, joinPath, Logger, normalize, resolvePath } from "@ts-common/azure-js-dev-tools";
import * as path from "path";
import * as yargs from "yargs";
const logger: Logger = getDefaultLogger({ logVerbose: true });
const logger: Logger = getDefaultLogger();
function getArgument(argumentName: string, environmentVariableName?: string, defaultValue?: string): string | undefined {
let rawArgument: string | string[] | undefined = yargs.argv[argumentName] || process.env[environmentVariableName || argumentName] || defaultValue;
if (Array.isArray(rawArgument)) {
rawArgument = rawArgument[rawArgument.length - 1];
}
return rawArgument;
}
const headReference: string | undefined = getArgument("head-reference", "headReference");
const baseReference: string | undefined = getArgument("base-reference", "baseReference");
const headReference: string | undefined = getArgument("head-reference", { environmentVariableName: "headReference" });
const baseReference: string | undefined = getArgument("base-reference", { environmentVariableName: "baseReference" });
const changedFiles: string[] = [];

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

@ -8,85 +8,86 @@ 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";
export function arrayContains<T>(array: T[], el: T): boolean {
return array.indexOf(el) != -1
return array.indexOf(el) != -1
}
export async function isDirectory(directoryPath: string): Promise<boolean> {
try {
const stats = await fs.lstat(directoryPath);
return stats.isDirectory();
} catch {
return false;
}
try {
const stats = await fs.lstat(directoryPath);
return stats.isDirectory();
} catch {
return false;
}
}
export async function pathExists(path: string): Promise<boolean> {
return new Promise<boolean>((resolve) => {
fssync.exists(path, exists => {
resolve(exists);
});
return new Promise<boolean>((resolve) => {
fssync.exists(path, exists => {
resolve(exists);
});
});
}
export function startsWith(value: string, prefix: string): boolean {
return !!(value && prefix && value.indexOf(prefix) === 0);
return !!(value && prefix && value.indexOf(prefix) === 0);
}
export function endsWith(value: string, suffix: string): boolean {
return !!(value && suffix && value.length >= suffix.length && value.lastIndexOf(suffix) === value.length - suffix.length);
return !!(value && suffix && value.length >= suffix.length && value.lastIndexOf(suffix) === value.length - suffix.length);
}
export function contains(values: string[], searchString: string): boolean {
return arrayContains(values, searchString);
return arrayContains(values, searchString);
}
export function execute(command: string, packageFolderPath: string): void {
if (fssync.existsSync(packageFolderPath)) {
execSync(command, { cwd: packageFolderPath, stdio: "inherit" });
}
if (fssync.existsSync(packageFolderPath)) {
execSync(command, { cwd: packageFolderPath, stdio: "inherit" });
}
}
export function npmRunBuild(packageFolderPath: string): void {
execute("npm run build", packageFolderPath);
execute("npm run build", packageFolderPath);
}
export function npmInstall(packageFolderPath: string): void {
execute("npm install", packageFolderPath);
execute("npm install", packageFolderPath);
}
export async function getChildDirectories(parent: string): Promise<string[]> {
const allChildren = await fs.readdir(parent);
const childDirectories = [];
const allChildren = await fs.readdir(parent);
const childDirectories = [];
for (const child of allChildren) {
if (await isDirectory(path.resolve(parent, child))) {
childDirectories.push(child);
}
for (const child of allChildren) {
if (await isDirectory(path.resolve(parent, child))) {
childDirectories.push(child);
}
}
return childDirectories;
return childDirectories;
}
export function findAzureRestApiSpecsRepositoryPathSync(): string | undefined {
const repositoryName = "azure-rest-api-specs";
let currentDirectory = __dirname;
const pathData = path.parse(currentDirectory);
const rootDirectory = pathData.root;
const repositoryName = "azure-rest-api-specs";
let currentDirectory = __dirname;
const pathData = path.parse(currentDirectory);
const rootDirectory = pathData.root;
do {
currentDirectory = path.resolve(currentDirectory, "..");
do {
currentDirectory = path.resolve(currentDirectory, "..");
if (containsDirectorySync(repositoryName, currentDirectory)) {
return path.resolve(currentDirectory, repositoryName);
}
if (containsDirectorySync(repositoryName, currentDirectory)) {
return path.resolve(currentDirectory, repositoryName);
}
} while (currentDirectory != rootDirectory);
} while (currentDirectory != rootDirectory);
return undefined;
return undefined;
}
function containsDirectorySync(directoryName: string, parentPath: string): boolean {
return fssync.existsSync(path.resolve(parentPath, directoryName));
}
return fssync.existsSync(path.resolve(parentPath, directoryName));
}

4
.scripts/latest.ts Normal file
Просмотреть файл

@ -0,0 +1,4 @@
import { resolvePath, changeClonedDependenciesTo } from "@ts-common/azure-js-dev-tools";
const packagePath: string = resolvePath(__dirname, "..");
changeClonedDependenciesTo(packagePath, "latest");

4
.scripts/local.ts Normal file
Просмотреть файл

@ -0,0 +1,4 @@
import { resolvePath, changeClonedDependenciesTo } from "@ts-common/azure-js-dev-tools";
const packagePath: string = resolvePath(__dirname, "..");
changeClonedDependenciesTo(packagePath, "local");

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

@ -25,7 +25,7 @@ steps:
inputs:
command: custom
verbose: false
customCommand: 'run check:everything -- --head-reference=origin/$(System.PullRequest.SourceBranch) --base-reference=origin/$(System.PullRequest.TargetBranch) --azure-devops'
customCommand: 'run check:everything -- --head-reference=origin/$(System.PullRequest.SourceBranch) --base-reference=origin/$(System.PullRequest.TargetBranch) --azure-devops --verbose'
- task: Npm@1
displayName: 'npm run build'

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

@ -4,7 +4,7 @@
* license information.
*/
import { contains, gitDiff, GitDiffResult, gitStatus, GitStatusResult, joinPath, normalize, npmInstall, npmRun, NPMScope, NPMViewResult, RunOptions, StringMap } from "@ts-common/azure-js-dev-tools";
import { contains, getArgument, gitDiff, GitDiffResult, gitStatus, GitStatusResult, joinPath, normalize, npmInstall, npmRun, NPMScope, NPMViewResult, RunOptions, StringMap } from "@ts-common/azure-js-dev-tools";
import * as fs from "fs";
import gulp from "gulp";
import * as path from "path";
@ -40,19 +40,11 @@ function getPackagesToPackArgument(toPackArgument: string | undefined): Packages
const args: CommandLineOptions = getCommandLineOptions();
const _logger: Logger = Logger.get();
function getArgument(argumentName: string, environmentVariableName?: string, defaultValue?: string): string | undefined {
let rawArgument: string | string[] | undefined = args[argumentName] || process.env[environmentVariableName || argumentName] || defaultValue;
if (Array.isArray(rawArgument)) {
rawArgument = rawArgument[rawArgument.length - 1];
}
return rawArgument;
}
const azureSDKForJSRepoRoot: string = getArgument("azure-sdk-for-js-repo-root", undefined, __dirname)!;
const azureSDKForJSRepoRoot: string = getArgument("azure-sdk-for-js-repo-root", { defaultValue: __dirname })!;
const rawToPack: string | undefined = getArgument("to-pack");
let toPack: PackagesToPack = getPackagesToPackArgument(rawToPack);
const headReference: string | undefined = getArgument("head-reference", "headReference");
const baseReference: string | undefined = getArgument("base-reference", "baseReference");
const headReference: string | undefined = getArgument("head-reference", { environmentVariableName: "headReference" });
const baseReference: string | undefined = getArgument("base-reference", { environmentVariableName: "baseReference" });
function getDropFolderPath(): string {
let result: string | undefined = getArgument("drop");

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

@ -30,6 +30,8 @@
"scripts": {
"build": "gulp pack --logging-level=info",
"check:everything": "ts-node ./.scripts/checkEverything.ts",
"latest": "ts-node ./.scripts/latest.ts",
"local": "ts-node ./.scripts/local.ts",
"install-client-keyvault": "cd packages/@azure/keyvault && npm install",
"install-client-template": "cd packages/@azure/template && npm install",
"install-client": "npm-run-all -p -l install-client-*",
@ -47,7 +49,7 @@
},
"devDependencies": {
"@octokit/rest": "15.17.0",
"@ts-common/azure-js-dev-tools": "^0.6.8",
"@ts-common/azure-js-dev-tools": "file:C:/Users/daschult/Sources/azure-js-dev-tools",
"@types/glob": "^7.1.1",
"@types/gulp": "^4.0.5",
"@types/js-yaml": "^3.11.2",