Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(check:policy): Add policy to check for correct dependency types #12724

Merged
merged 7 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build-tools/packages/build-cli/src/commands/check/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ export class CheckPolicy extends BaseCommand<typeof CheckPolicy.flags> {

try {
this.routeToHandlers(filePath, handlerRegex, handlers);
} catch {
throw new Error("Line error");
} catch (error: any) {
throw new Error(`Line error: ${error}`);
}

CheckPolicy.processed++;
Expand Down
2 changes: 1 addition & 1 deletion build-tools/packages/build-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"@types/json5": "^0.0.30",
"@types/lodash.isequal": "^4.5.5",
"@types/lodash.merge": "^4.6.6",
"@types/minimatch": "3.0.5",
"@types/minimatch": "^3.0.5",
"@types/mocha": "^9.0.0",
"@types/node": "^14.18.0",
"@types/rimraf": "^2.0.3",
Expand Down
14 changes: 13 additions & 1 deletion build-tools/packages/build-tools/src/common/fluidRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@ import * as path from "path";
import { getPackageManifest } from "./fluidUtils";
import { Logger, defaultLogger } from "./logging";
import { MonoRepo, MonoRepoKind, isMonoRepoKind } from "./monoRepo";
import { Package, Packages } from "./npmPackage";
import { Package, Packages, ScriptDependencies } from "./npmPackage";
import { ExecAsyncResult } from "./utils";

export interface IPackageManifest {
repoPackages: {
[name: string]: IFluidRepoPackageEntry;
};
buildDependencies?: {
merge?: {
[key: string]: ScriptDependencies;
};
};
generatorName?: string;
policy?: PolicyConfig;
}

export interface PolicyConfig {
dependencies?: {
requireTilde?: string[];
};
}

export interface IFluidRepoPackage {
Expand Down
10 changes: 7 additions & 3 deletions build-tools/packages/build-tools/src/common/fluidUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as path from "path";
import { commonOptions } from "./commonOptions";
import { IPackageManifest } from "./fluidRepo";
import { defaultLogger } from "./logging";
import { existsSync, lookUpDirAsync, readJsonAsync, realpathAsync } from "./utils";
import { existsSync, lookUpDirAsync, readJsonAsync, readJsonSync, realpathAsync } from "./utils";

const { verbose } = defaultLogger;

Expand Down Expand Up @@ -103,6 +103,10 @@ export async function getResolvedFluidRoot() {
}

export function getPackageManifest(rootDir: string): IPackageManifest {
const pkgString = fs.readFileSync(`${rootDir}/package.json`);
return JSON.parse(pkgString as any).fluidBuild;
const jsonPath = path.join(rootDir, "package.json");
if (!existsSync(jsonPath)) {
throw new Error(`Root package.json not found in: ${rootDir}`);
}
const pkgJson = readJsonSync(jsonPath);
return pkgJson?.fluidBuild;
}
11 changes: 3 additions & 8 deletions build-tools/packages/build-tools/src/common/npmPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as path from "path";
import sortPackageJson from "sort-package-json";

import { options } from "../fluidBuild/options";
import { IPackageManifest } from "./fluidRepo";
import { defaultLogger } from "./logging";
import { MonoRepo, MonoRepoKind } from "./monoRepo";
import {
Expand All @@ -34,7 +35,7 @@ interface IPerson {
url: string;
}

interface IPackage {
export interface IPackage {
name: string;
version: string;
private: boolean;
Expand Down Expand Up @@ -64,13 +65,7 @@ interface IPackage {
cpu: string[];
[key: string]: any;

fluidBuild?: {
buildDependencies: {
merge?: {
[key: string]: ScriptDependencies;
};
};
};
fluidBuild?: IPackageManifest;
}

export class Package {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import * as readline from "readline";
import replace from "replace-in-file";
import sortPackageJson from "sort-package-json";

import { IPackageManifest } from "../../common/fluidRepo";
import { getPackageManifest } from "../../common/fluidUtils";
import { IPackage } from "../../common/npmPackage";
import { Handler, readFile, writeFile } from "../common";

const licenseId = "MIT";
Expand All @@ -27,6 +30,18 @@ or logos must follow Microsoft's [Trademark & Brand Guidelines](https://www.micr
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
`;

/**
* An array of dependencies that should always use tilde dependency ranges instead of caret.
*/
let _tildeDependencies: string[] | undefined;

const getTildeDependencies = (manifest: IPackageManifest | undefined) => {
if (_tildeDependencies === undefined) {
_tildeDependencies = manifest?.policy?.dependencies?.requireTilde ?? [];
}
return _tildeDependencies;
};

// Some of our package scopes definitely should publish, and others should never publish. If they should never
// publish, we want to add the "private": true flag to their package.json to prevent publishing, and conversely if
// they should always publish we should ensure the "private": true flag is not present. There is then a third
Expand Down Expand Up @@ -481,9 +496,9 @@ export const handlers: Handler[] = [
{
name: "npm-package-json-lint",
match,
handler: (file) => {
handler: (file, root) => {
let jsonStr: string;
let json;
let json: IPackage;
try {
jsonStr = readFile(file);
json = JSON.parse(jsonStr);
Expand All @@ -493,6 +508,25 @@ export const handlers: Handler[] = [

const ret: string[] = [];

const { dependencies, devDependencies } = json;
const manifest = getPackageManifest(root);
const tildeDependencies = getTildeDependencies(manifest);
if (dependencies !== undefined) {
for (const [dep, ver] of Object.entries(json.dependencies)) {
if (tildeDependencies.includes(dep) && !ver.startsWith("~")) {
ret.push(`Dependencies on ${dep} must use tilde (~) dependencies.`);
}
}
}

if (devDependencies !== undefined) {
for (const [dep, ver] of Object.entries(json.devDependencies)) {
if (tildeDependencies.includes(dep) && !ver.startsWith("~")) {
ret.push(`devDependencies on ${dep} must use tilde (~) dependencies.`);
}
}
}

const { valid, validationResults } = runNpmJsonLint(json, file);

if (!valid) {
Expand Down
2 changes: 1 addition & 1 deletion experimental/dds/tree-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"eslint": "~8.6.0",
"mocha": "^10.0.0",
"nyc": "^15.0.0",
"prettier": "^2.3.1",
"prettier": "~2.3.1",
"rimraf": "^2.6.2",
"typescript": "~4.5.5"
},
Expand Down
2 changes: 1 addition & 1 deletion experimental/dds/tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"eslint": "~8.6.0",
"mocha": "^10.0.0",
"nyc": "^15.0.0",
"prettier": "^2.3.1",
"prettier": "~2.3.1",
"random-js": "^1.0.8",
"rimraf": "^2.6.2",
"typescript": "~4.5.5"
Expand Down
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@
"tinylicious"
]
}
},
"policy": {
"dependencies": {
"requireTilde": [
"@typescript-eslint/eslint-plugin",
"@typescript-eslint/parser",
"eslint-config-prettier",
"eslint-plugin-eslint-comments",
"eslint-plugin-import",
"eslint-plugin-unicorn",
"eslint-plugin-unused-imports",
"eslint",
"prettier",
"typescript",
"webpack-dev-server"
]
}
}
}
}
2 changes: 1 addition & 1 deletion packages/dds/tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"good-fences": "^1.1.1",
"mocha": "^10.0.0",
"nyc": "^15.0.0",
"prettier": "^2.3.1",
"prettier": "~2.3.1",
"rimraf": "^2.6.2",
"source-map-support": "^0.5.16",
"typescript": "~4.5.5",
Expand Down