@dotcom-tool-kit/package-json-hook 2.1.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,27 @@
1
1
  import { Hook } from '@dotcom-tool-kit/types';
2
- import type { PackageJson } from '@financial-times/package-json';
3
- export declare abstract class PackageJsonHelper extends Hook {
4
- _packageJson?: PackageJson;
5
- abstract field: string;
2
+ interface PackageJson {
3
+ [field: string]: PackageJson | string;
4
+ }
5
+ interface PackageJsonStateValue {
6
+ hooks: string[];
7
+ trailingString: string;
8
+ }
9
+ interface PackageJsonState {
10
+ [field: string]: PackageJsonState | PackageJsonStateValue;
11
+ }
12
+ export declare abstract class PackageJsonHelper extends Hook<PackageJsonState> {
13
+ private _packageJson?;
14
+ abstract field: string | string[];
6
15
  abstract key: string;
7
16
  abstract hook: string;
8
- get packageJson(): PackageJson;
17
+ trailingString?: string;
18
+ installGroup: string;
19
+ filepath: string;
20
+ getPackageJson(): Promise<PackageJson>;
21
+ private get hookPath();
9
22
  check(): Promise<boolean>;
10
- abstract install(): Promise<void>;
23
+ install(state?: PackageJsonState): Promise<PackageJsonState>;
24
+ commitInstall(state: PackageJsonState): Promise<void>;
11
25
  }
26
+ export {};
12
27
  //# sourceMappingURL=package-json-helper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"package-json-helper.d.ts","sourceRoot":"","sources":["../src/package-json-helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAC7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAA;AAQhE,8BAAsB,iBAAkB,SAAQ,IAAI;IACnD,YAAY,CAAC,EAAE,WAAW,CAAA;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IAErB,IAAI,WAAW,IAAI,WAAW,CAO7B;IAEK,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC;IAK/B,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAChC"}
1
+ {"version":3,"file":"package-json-helper.d.ts","sourceRoot":"","sources":["../src/package-json-helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAQ7C,UAAU,WAAW;IACnB,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,GAAG,MAAM,CAAA;CACtC;AAED,UAAU,qBAAqB;IAC7B,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,cAAc,EAAE,MAAM,CAAA;CACvB;AAED,UAAU,gBAAgB;IACxB,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB,GAAG,qBAAqB,CAAA;CAC1D;AAED,8BAAsB,iBAAkB,SAAQ,IAAI,CAAC,gBAAgB,CAAC;IACpE,OAAO,CAAC,YAAY,CAAC,CAAa;IAClC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IACjC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IAIrB,cAAc,CAAC,EAAE,MAAM,CAAA;IAEvB,YAAY,SAAiB;IAE7B,QAAQ,SAA8C;IAEhD,cAAc,IAAI,OAAO,CAAC,WAAW,CAAC;IAW5C,OAAO,KAAK,QAAQ,GAEnB;IAEK,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC;IAKzB,OAAO,CAAC,KAAK,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAU5D,aAAa,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC;CAa5D"}
@@ -3,20 +3,53 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PackageJsonHelper = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const types_1 = require("@dotcom-tool-kit/types");
6
- const package_json_1 = (0, tslib_1.__importDefault)(require("@financial-times/package-json"));
7
- const path_1 = (0, tslib_1.__importDefault)(require("path"));
6
+ const fs_1 = tslib_1.__importDefault(require("fs"));
7
+ const get_1 = tslib_1.__importDefault(require("lodash/get"));
8
+ const mapValues_1 = tslib_1.__importDefault(require("lodash/mapValues"));
9
+ const merge_1 = tslib_1.__importDefault(require("lodash/merge"));
10
+ const update_1 = tslib_1.__importDefault(require("lodash/update"));
11
+ const path_1 = tslib_1.__importDefault(require("path"));
8
12
  class PackageJsonHelper extends types_1.Hook {
9
- get packageJson() {
13
+ constructor() {
14
+ super(...arguments);
15
+ this.installGroup = 'package-json';
16
+ this.filepath = path_1.default.resolve(process.cwd(), 'package.json');
17
+ }
18
+ async getPackageJson() {
10
19
  if (!this._packageJson) {
11
- const filepath = path_1.default.resolve(process.cwd(), 'package.json');
12
- this._packageJson = (0, package_json_1.default)({ filepath });
20
+ const rawPackageJson = await fs_1.default.promises.readFile(this.filepath, 'utf8');
21
+ const packageJson = JSON.parse(rawPackageJson);
22
+ this._packageJson = packageJson;
23
+ return packageJson;
13
24
  }
14
25
  return this._packageJson;
15
26
  }
27
+ get hookPath() {
28
+ return Array.isArray(this.field) ? [...this.field, this.key] : [this.field, this.key];
29
+ }
16
30
  async check() {
17
31
  var _a;
18
- const commands = this.packageJson.getField(this.field);
19
- return (_a = commands === null || commands === void 0 ? void 0 : commands[this.key]) === null || _a === void 0 ? void 0 : _a.includes(this.hook);
32
+ const packageJson = await this.getPackageJson();
33
+ return (_a = (0, get_1.default)(packageJson, this.hookPath)) === null || _a === void 0 ? void 0 : _a.includes(this.hook);
34
+ }
35
+ async install(state) {
36
+ state !== null && state !== void 0 ? state : (state = {});
37
+ // prepend each hook to maintain the same order as previous implementations
38
+ (0, update_1.default)(state, this.hookPath, (hookState) => {
39
+ var _a;
40
+ return ({
41
+ hooks: [this.hook, ...((_a = hookState === null || hookState === void 0 ? void 0 : hookState.hooks) !== null && _a !== void 0 ? _a : [])],
42
+ trailingString: this.trailingString
43
+ });
44
+ });
45
+ return state;
46
+ }
47
+ async commitInstall(state) {
48
+ const reduceHooks = (state) => (0, mapValues_1.default)(state, (field) => Array.isArray(field === null || field === void 0 ? void 0 : field.hooks)
49
+ ? `dotcom-tool-kit ${field.hooks.join(' ')}${field.trailingString ? ' ' + field.trailingString : ''}`
50
+ : reduceHooks(field));
51
+ const newPackageJson = (0, merge_1.default)(await this.getPackageJson(), reduceHooks(state));
52
+ await fs_1.default.promises.writeFile(this.filepath, JSON.stringify(newPackageJson, null, 2) + '\n');
20
53
  }
21
54
  }
22
55
  exports.PackageJsonHelper = PackageJsonHelper;
@@ -1,6 +1,5 @@
1
1
  import { PackageJsonHelper } from './package-json-helper';
2
2
  export declare abstract class PackageJsonScriptHook extends PackageJsonHelper {
3
3
  field: string;
4
- install(): Promise<void>;
5
4
  }
6
5
  //# sourceMappingURL=script-hook.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"script-hook.d.ts","sourceRoot":"","sources":["../src/script-hook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAMzD,8BAAsB,qBAAsB,SAAQ,iBAAiB;IACnE,KAAK,SAAY;IAEX,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAa/B"}
1
+ {"version":3,"file":"script-hook.d.ts","sourceRoot":"","sources":["../src/script-hook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAEzD,8BAAsB,qBAAsB,SAAQ,iBAAiB;IACnE,KAAK,SAAY;CAClB"}
@@ -7,17 +7,5 @@ class PackageJsonScriptHook extends package_json_helper_1.PackageJsonHelper {
7
7
  super(...arguments);
8
8
  this.field = 'scripts';
9
9
  }
10
- async install() {
11
- let command = `dotcom-tool-kit ${this.hook}`;
12
- const existingCommand = this.packageJson.getField(this.field)[this.key];
13
- if (existingCommand && existingCommand.startsWith('dotcom-tool-kit ')) {
14
- command = command.concat(existingCommand.replace('dotcom-tool-kit', ''));
15
- }
16
- this.packageJson.requireScript({
17
- stage: this.key,
18
- command
19
- });
20
- this.packageJson.writeChanges();
21
- }
22
10
  }
23
11
  exports.PackageJsonScriptHook = PackageJsonScriptHook;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotcom-tool-kit/package-json-hook",
3
- "version": "2.1.1",
3
+ "version": "4.0.0",
4
4
  "description": "",
5
5
  "main": "lib",
6
6
  "scripts": {
@@ -11,6 +11,7 @@
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
13
  "@financial-times/package-json": "^3.0.0",
14
+ "lodash": "^4.17.21",
14
15
  "tslib": "^2.3.1"
15
16
  },
16
17
  "repository": {
@@ -22,6 +23,7 @@
22
23
  "homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/lib/package-json-hook",
23
24
  "devDependencies": {
24
25
  "@jest/globals": "^27.4.6",
26
+ "@types/lodash": "^4.14.185",
25
27
  "winston": "^3.5.1"
26
28
  },
27
29
  "files": [