presetter 3.1.1 → 3.4.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.
package/lib/index.js CHANGED
@@ -1,71 +1,23 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _content = require("./content");
8
-
9
- Object.keys(_content).forEach(function (key) {
10
- if (key === "default" || key === "__esModule") return;
11
- if (key in exports && exports[key] === _content[key]) return;
12
- Object.defineProperty(exports, key, {
13
- enumerable: true,
14
- get: function () {
15
- return _content[key];
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
16
7
  }
17
- });
18
- });
19
-
20
- var _directive = require("./directive");
21
-
22
- Object.keys(_directive).forEach(function (key) {
23
- if (key === "default" || key === "__esModule") return;
24
- if (key in exports && exports[key] === _directive[key]) return;
25
- Object.defineProperty(exports, key, {
26
- enumerable: true,
27
- get: function () {
28
- return _directive[key];
29
- }
30
- });
31
- });
32
-
33
- var _io = require("./io");
34
-
35
- Object.keys(_io).forEach(function (key) {
36
- if (key === "default" || key === "__esModule") return;
37
- if (key in exports && exports[key] === _io[key]) return;
38
- Object.defineProperty(exports, key, {
39
- enumerable: true,
40
- get: function () {
41
- return _io[key];
42
- }
43
- });
44
- });
45
-
46
- var _template = require("./template");
47
-
48
- Object.keys(_template).forEach(function (key) {
49
- if (key === "default" || key === "__esModule") return;
50
- if (key in exports && exports[key] === _template[key]) return;
51
- Object.defineProperty(exports, key, {
52
- enumerable: true,
53
- get: function () {
54
- return _template[key];
55
- }
56
- });
57
- });
58
-
59
- var _types = require("./types");
60
-
61
- Object.keys(_types).forEach(function (key) {
62
- if (key === "default" || key === "__esModule") return;
63
- if (key in exports && exports[key] === _types[key]) return;
64
- Object.defineProperty(exports, key, {
65
- enumerable: true,
66
- get: function () {
67
- return _types[key];
68
- }
69
- });
70
- });
71
- //# sourceMappingURL=index.js.map
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./content"), exports);
18
+ __exportStar(require("./directive"), exports);
19
+ __exportStar(require("./io"), exports);
20
+ __exportStar(require("./resolution"), exports);
21
+ __exportStar(require("./template"), exports);
22
+ __exportStar(require("./types"), exports);
23
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zb3VyY2UvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7OztBQWlCQSwyQ0FBeUI7QUFDekIsNkNBQTJCO0FBQzNCLHNDQUFvQjtBQUNwQiw4Q0FBNEI7QUFDNUIsNENBQTBCO0FBRTFCLHlDQUF1QiJ9
package/lib/io.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- import type { Generator, PresetterConfig, ResolvedPresetContext, Template } from './types';
2
- export declare function loadDynamic<R extends Template | string[], K extends keyof PresetterConfig>(value: string | R | Generator<R, K>, context: ResolvedPresetContext<K>): Promise<R>;
1
+ import type { Template } from './types';
3
2
  export declare function loadFile(path: string, defaultFormat?: 'json' | 'yaml'): Promise<Record<string, unknown>>;
4
3
  export declare function loadFile(path: string, defaultFormat?: 'text'): Promise<string>;
5
- export declare function serialiseContent(destination: string, content: Template): string;
4
+ export declare function serializeContent(destination: string, content: Template): string;
6
5
  export declare function writeFiles(root: string, config: Record<string, Template>, pathMap: Record<string, string>): Promise<void>;
7
6
  export declare function linkFiles(root: string, configurationLink: Record<string, string>): Promise<void>;
8
7
  export declare function unlinkFiles(root: string, configurationLink: Record<string, string>): Promise<void>;
package/lib/io.js CHANGED
@@ -1,188 +1,94 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.linkFiles = linkFiles;
7
- exports.loadDynamic = loadDynamic;
8
- exports.loadFile = loadFile;
9
- exports.serialiseContent = serialiseContent;
10
- exports.unlinkFiles = unlinkFiles;
11
- exports.writeFiles = writeFiles;
12
-
13
- var _console = require("console");
14
-
15
- var _fsExtra = require("fs-extra");
16
-
17
- var _jsYaml = require("js-yaml");
18
-
19
- var _path = require("path");
20
-
21
- /*
22
- * *** MIT LICENSE ***
23
- * -------------------------------------------------------------------------
24
- * This code may be modified and distributed under the MIT license.
25
- * See the LICENSE file for details.
26
- * -------------------------------------------------------------------------
27
- *
28
- * @summary Collection of io helpers
29
- *
30
- * @author Alvis HT Tang <alvis@hilbert.space>
31
- * @license MIT
32
- * @copyright Copyright (c) 2020 - All Rights Reserved.
33
- * -------------------------------------------------------------------------
34
- */
35
- // JSON format
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unlinkFiles = exports.linkFiles = exports.writeFiles = exports.serializeContent = exports.loadFile = void 0;
4
+ const console_1 = require("console");
5
+ const fs_extra_1 = require("fs-extra");
6
+ const js_yaml_1 = require("js-yaml");
7
+ const path_1 = require("path");
36
8
  const INDENT = 2;
37
- /**
38
- * load a potentially dynamic content
39
- * @param value content to be loaded
40
- * @param context context to be supplied to the generator
41
- * @returns resolved content
42
- */
43
-
44
- async function loadDynamic(value, context) {
45
- if (typeof value === 'function') {
46
- return value(context);
47
- } else if (typeof value === 'string' && (await (0, _fsExtra.pathExists)(value))) {
48
- return await loadFile(value);
49
- } else {
50
- return value;
51
- }
52
- }
53
- /**
54
- * load the content of a file
55
- * @param path file path to be loaded
56
- * @param defaultFormat default format of the file
57
- * @returns content of the file
58
- */
59
-
60
-
61
9
  async function loadFile(path, defaultFormat = 'text') {
62
- const content = (await (0, _fsExtra.readFile)(path)).toString(); // parse the content depending on the extension
63
-
64
- switch ((0, _path.extname)(path) || `.${defaultFormat}`) {
65
- case '.json':
66
- return JSON.parse(content);
67
-
68
- case '.yaml':
69
- case '.yml':
70
- return (0, _jsYaml.load)(content);
71
-
72
- default:
73
- return content;
74
- }
10
+ const content = (await (0, fs_extra_1.readFile)(path)).toString();
11
+ switch ((0, path_1.extname)(path) || `.${defaultFormat}`) {
12
+ case '.json':
13
+ return JSON.parse(content);
14
+ case '.yaml':
15
+ case '.yml':
16
+ return (0, js_yaml_1.load)(content);
17
+ default:
18
+ return content;
19
+ }
75
20
  }
76
- /**
77
- * serialise a configuration content to the right format according to its destination
78
- * @param destination the path to which the content will be written
79
- * @param content configuration content
80
- * @returns serialised content
81
- */
82
-
83
-
84
- function serialiseContent(destination, content) {
85
- if (typeof content === 'string') {
86
- return content;
87
- }
88
-
89
- switch ((0, _path.extname)(destination)) {
90
- case '.yaml':
91
- case '.yml':
92
- return (0, _jsYaml.dump)(content);
93
-
94
- case '.json':
95
- default:
96
- return JSON.stringify(content, null, INDENT);
97
- }
21
+ exports.loadFile = loadFile;
22
+ function serializeContent(destination, content) {
23
+ if (typeof content === 'string') {
24
+ return content;
25
+ }
26
+ switch ((0, path_1.extname)(destination)) {
27
+ case '.yaml':
28
+ case '.yml':
29
+ return (0, js_yaml_1.dump)(content);
30
+ case '.json':
31
+ default:
32
+ return JSON.stringify(content, null, INDENT);
33
+ }
98
34
  }
99
- /**
100
- * write all generated configuration to their destination
101
- * @param root path to the target project root
102
- * @param config a map of configuration content and its path to be written
103
- * @param pathMap a map of keys in the config map and their destination path
104
- */
105
-
106
-
35
+ exports.serializeContent = serializeContent;
107
36
  async function writeFiles(root, config, pathMap) {
108
- for (const [key, content] of Object.entries(config)) {
109
- const destination = pathMap[key]; // write content to the destination path
110
-
111
- if ( // file don't exist
112
- !(await (0, _fsExtra.pathExists)(destination)) || // content to be written under the configurations folder
113
- destination !== (0, _path.resolve)(root, key)) {
114
- (0, _console.info)(`Generating ${key}`); // ensure that all parent folders exist to avoid errors from writeFile
115
-
116
- await (0, _fsExtra.ensureFile)(destination); // write content to the destination path
117
-
118
- await (0, _fsExtra.writeFile)(destination, serialiseContent(destination, content));
119
- } else {
120
- (0, _console.info)(`Skipping ${key}`);
37
+ for (const [key, content] of Object.entries(config)) {
38
+ const destination = pathMap[key];
39
+ if (!(await (0, fs_extra_1.pathExists)(destination)) ||
40
+ destination !== (0, path_1.resolve)(root, key)) {
41
+ (0, console_1.info)(`Generating ${key}`);
42
+ await (0, fs_extra_1.ensureFile)(destination);
43
+ await (0, fs_extra_1.writeFile)(destination, serializeContent(destination, content));
44
+ }
45
+ else {
46
+ (0, console_1.info)(`Skipping ${key}`);
47
+ }
121
48
  }
122
- }
123
49
  }
124
- /**
125
- * link generated files to the project root
126
- * @param root path to the target project root
127
- * @param configurationLink map of symlinks to its real path
128
- */
129
-
130
-
50
+ exports.writeFiles = writeFiles;
131
51
  async function linkFiles(root, configurationLink) {
132
- for (const [file, destination] of Object.entries(configurationLink)) {
133
- const link = (0, _path.resolve)(root, file);
134
- const to = (0, _path.relative)((0, _path.dirname)(link), destination); // create links only if the path really doesn't exist
135
-
136
- if (!(await linkExists(link)) && !(await (0, _fsExtra.pathExists)(link)) && // for files that mean to be created directly on the target project root, not via symlink
137
- to !== (0, _path.basename)(to)) {
138
- (0, _console.info)(`Linking ${(0, _path.relative)(root, link)} => ${to}`);
139
- await (0, _fsExtra.mkdir)((0, _path.dirname)(link), {
140
- recursive: true
141
- });
142
- await (0, _fsExtra.symlink)(to, link);
143
- } else if (to !== (0, _path.basename)(to)) {
144
- (0, _console.info)(`Skipping ${(0, _path.relative)(root, link)} => ${to}`);
52
+ for (const [file, destination] of Object.entries(configurationLink)) {
53
+ const link = (0, path_1.resolve)(root, file);
54
+ const to = (0, path_1.relative)((0, path_1.dirname)(link), destination);
55
+ if (!(await linkExists(link)) &&
56
+ !(await (0, fs_extra_1.pathExists)(link)) &&
57
+ to !== (0, path_1.basename)(to)) {
58
+ (0, console_1.info)(`Linking ${(0, path_1.relative)(root, link)} => ${to}`);
59
+ await (0, fs_extra_1.mkdir)((0, path_1.dirname)(link), { recursive: true });
60
+ await (0, fs_extra_1.symlink)(to, link);
61
+ }
62
+ else if (to !== (0, path_1.basename)(to)) {
63
+ (0, console_1.info)(`Skipping ${(0, path_1.relative)(root, link)} => ${to}`);
64
+ }
145
65
  }
146
- }
147
66
  }
148
- /**
149
- * unlink generated files from the project root
150
- * @param root path to the target project root
151
- * @param configurationLink map of symlinks to its real path
152
- */
153
-
154
-
67
+ exports.linkFiles = linkFiles;
155
68
  async function unlinkFiles(root, configurationLink) {
156
- for (const [name, destination] of Object.entries(configurationLink)) {
157
- try {
158
- const link = await (0, _fsExtra.readlink)((0, _path.resolve)(root, name));
159
- const to = (0, _path.relative)(root, destination);
160
-
161
- if (link === to) {
162
- (0, _console.info)(`Removing ${name}`);
163
- await (0, _fsExtra.unlink)((0, _path.resolve)(root, name));
164
- continue;
165
- }
166
- } catch {// do nothing
69
+ for (const [name, destination] of Object.entries(configurationLink)) {
70
+ try {
71
+ const link = await (0, fs_extra_1.readlink)((0, path_1.resolve)(root, name));
72
+ const to = (0, path_1.relative)(root, destination);
73
+ if (link === to) {
74
+ (0, console_1.info)(`Removing ${name}`);
75
+ await (0, fs_extra_1.unlink)((0, path_1.resolve)(root, name));
76
+ continue;
77
+ }
78
+ }
79
+ catch (_a) {
80
+ }
81
+ (0, console_1.info)(`Skipping ${name}`);
167
82
  }
168
-
169
- (0, _console.info)(`Skipping ${name}`);
170
- }
171
83
  }
172
- /**
173
- * check if path is a symlink
174
- * @param path file path to be checked
175
- * @returns true if it is a symlink
176
- */
177
-
178
-
84
+ exports.unlinkFiles = unlinkFiles;
179
85
  async function linkExists(path) {
180
- try {
181
- // NOTE use lstat instead of pathExists as it checks the link not the linked path
182
- await (0, _fsExtra.lstat)(path);
183
- return true;
184
- } catch {
185
- return false;
186
- }
86
+ try {
87
+ await (0, fs_extra_1.lstat)(path);
88
+ return true;
89
+ }
90
+ catch (_a) {
91
+ return false;
92
+ }
187
93
  }
188
- //# sourceMappingURL=io.js.map
94
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW8uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zb3VyY2UvaW8udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBZUEscUNBQStCO0FBQy9CLHVDQVVrQjtBQUNsQixxQ0FBcUM7QUFDckMsK0JBQXFFO0FBS3JFLE1BQU0sTUFBTSxHQUFHLENBQUMsQ0FBQztBQWdCVixLQUFLLFVBQVUsUUFBUSxDQUM1QixJQUFZLEVBQ1osZ0JBQTBDLE1BQU07SUFFaEQsTUFBTSxPQUFPLEdBQUcsQ0FBQyxNQUFNLElBQUEsbUJBQVEsRUFBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLFFBQVEsRUFBRSxDQUFDO0lBR2xELFFBQVEsSUFBQSxjQUFPLEVBQUMsSUFBSSxDQUFDLElBQUksSUFBSSxhQUFhLEVBQUUsRUFBRTtRQUM1QyxLQUFLLE9BQU87WUFDVixPQUFPLElBQUksQ0FBQyxLQUFLLENBQUMsT0FBTyxDQUE0QixDQUFDO1FBQ3hELEtBQUssT0FBTyxDQUFDO1FBQ2IsS0FBSyxNQUFNO1lBQ1QsT0FBTyxJQUFBLGNBQUksRUFBQyxPQUFPLENBQTRCLENBQUM7UUFDbEQ7WUFDRSxPQUFPLE9BQU8sQ0FBQztLQUNsQjtBQUNILENBQUM7QUFoQkQsNEJBZ0JDO0FBUUQsU0FBZ0IsZ0JBQWdCLENBQzlCLFdBQW1CLEVBQ25CLE9BQWlCO0lBRWpCLElBQUks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ssQ0FBQztLQUNkO0FBQ0gsQ0FBQyJ9
package/lib/package.js CHANGED
@@ -1,159 +1,68 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.arePeerPackagesAutoInstalled = arePeerPackagesAutoInstalled;
7
- exports.getPackage = getPackage;
8
- exports.reifyDependencies = reifyDependencies;
9
-
10
- var _arborist = require("@npmcli/arborist");
11
-
12
- var _config = _interopRequireDefault(require("@npmcli/config"));
13
-
14
- var _lodash = require("lodash");
15
-
16
- var _os = require("os");
17
-
18
- var _path = require("path");
19
-
20
- var _readPkgUp = _interopRequireDefault(require("read-pkg-up"));
21
-
22
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
24
- /*
25
- * *** MIT LICENSE ***
26
- * -------------------------------------------------------------------------
27
- * This code may be modified and distributed under the MIT license.
28
- * See the LICENSE file for details.
29
- * -------------------------------------------------------------------------
30
- *
31
- * @summary Helpers for handling a package
32
- *
33
- * @author Alvis HT Tang <alvis@hilbert.space>
34
- * @license MIT
35
- * @copyright Copyright (c) 2019 - All Rights Reserved.
36
- * -------------------------------------------------------------------------
37
- */
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.reifyDependencies = exports.getPackage = exports.arePeerPackagesAutoInstalled = void 0;
7
+ const arborist_1 = require("@npmcli/arborist");
8
+ const config_1 = __importDefault(require("@npmcli/config"));
9
+ const lodash_1 = require("lodash");
10
+ const os_1 = require("os");
11
+ const path_1 = require("path");
12
+ const read_pkg_up_1 = __importDefault(require("read-pkg-up"));
38
13
  const NPM_VERSION_FOR_PEER_INSTALLATION = 7;
39
- /** package detail */
40
-
41
- /**
42
- * indicate whether peer packages would be installed automatically
43
- * @returns true for npm v7+
44
- */
45
14
  function arePeerPackagesAutoInstalled() {
46
- var _process$env$npm_conf, _process$env$npm_conf2, _clientID$split, _version$split;
47
-
48
- // NOTE npm_config_user_agent should be in the form of 'npm/7.3.0 node/v15.5.0 darwin x64'
49
-
50
- /* istanbul ignore next */
51
- const [clientID] = (_process$env$npm_conf = (_process$env$npm_conf2 = process.env['npm_config_user_agent']) === null || _process$env$npm_conf2 === void 0 ? void 0 : _process$env$npm_conf2.split(' ')) !== null && _process$env$npm_conf !== void 0 ? _process$env$npm_conf : [];
52
- /* istanbul ignore next */
53
-
54
- const [name, version] = (_clientID$split = clientID === null || clientID === void 0 ? void 0 : clientID.split('/')) !== null && _clientID$split !== void 0 ? _clientID$split : [];
55
- /* istanbul ignore next */
56
-
57
- const [major] = (_version$split = version === null || version === void 0 ? void 0 : version.split('.')) !== null && _version$split !== void 0 ? _version$split : [];
58
- return name === 'npm' && major !== undefined && parseInt(major) >= NPM_VERSION_FOR_PEER_INSTALLATION;
15
+ var _a, _b, _c, _d;
16
+ const [clientID] = (_b = (_a = process.env['npm_config_user_agent']) === null || _a === void 0 ? void 0 : _a.split(' ')) !== null && _b !== void 0 ? _b : [];
17
+ const [name, version] = (_c = clientID === null || clientID === void 0 ? void 0 : clientID.split('/')) !== null && _c !== void 0 ? _c : [];
18
+ const [major] = (_d = version === null || version === void 0 ? void 0 : version.split('.')) !== null && _d !== void 0 ? _d : [];
19
+ return (name === 'npm' &&
20
+ major !== undefined &&
21
+ parseInt(major) >= NPM_VERSION_FOR_PEER_INSTALLATION);
59
22
  }
60
- /**
61
- * get scripts from the targeted project's package.json
62
- * @param root path to the project's root folder
63
- * @returns package detail
64
- */
65
-
66
-
23
+ exports.arePeerPackagesAutoInstalled = arePeerPackagesAutoInstalled;
67
24
  async function getPackage(root) {
68
- // try to find the target project's package.json
69
- const detail = await (0, _readPkgUp.default)({
70
- cwd: root,
71
- normalize: true
72
- }); // throw an error if there's no package.json found
73
-
74
- if (!detail) {
75
- throw new Error("failed to find target's package.json");
76
- }
77
-
78
- const path = detail.path;
79
- const json = (0, _lodash.defaultsDeep)(detail.packageJson, {
80
- scripts: {}
81
- });
82
- return {
83
- path,
84
- json
85
- };
25
+ const detail = await (0, read_pkg_up_1.default)({ cwd: root, normalize: true });
26
+ if (!detail) {
27
+ throw new Error("failed to find target's package.json");
28
+ }
29
+ const path = detail.path;
30
+ const json = (0, lodash_1.defaultsDeep)(detail.packageJson, {
31
+ scripts: {},
32
+ });
33
+ return { path, json };
86
34
  }
87
- /**
88
- * reify packages for the target project
89
- * @param args list of packages and other options
90
- * @param args.root directory of the project package.json
91
- * @param args.add list of packages to be added
92
- * @param args.remove list of packages to be removed
93
- * @param args.saveAs type of dependency
94
- * @param args.lockFile indicate whether package-lock.json should be produced
95
- * @returns list of installed packages
96
- */
97
-
98
-
35
+ exports.getPackage = getPackage;
99
36
  async function reifyDependencies(args) {
100
- const {
101
- root,
102
- add = [],
103
- remove = [],
104
- saveAs = 'prod',
105
- lockFile = false
106
- } = { ...args
107
- }; // use arborist to install peer dependencies
108
-
109
- const config = await getNPMConfig();
110
- const workspacesEnabled = arePeerPackagesAutoInstalled(); // no workspace before npm 7
111
-
112
- const arborist = new _arborist.Arborist({
113
- path: root,
114
- workspacesEnabled,
115
- ...config
116
- }); // don't write to the lockfile
117
-
118
- const actualTree = await arborist.reify({
119
- add,
120
- rm: remove,
121
- saveType: saveAs,
122
- save: lockFile,
123
- update: true
124
- });
125
- return [...actualTree.edgesOut.values()].map(edge => ({
126
- name: edge.name,
127
- spec: edge.spec
128
- }));
37
+ const { root, add = [], remove = [], saveAs = 'prod', lockFile = false, } = Object.assign({}, args);
38
+ const config = await getNPMConfig();
39
+ const workspacesEnabled = arePeerPackagesAutoInstalled();
40
+ const arborist = new arborist_1.Arborist(Object.assign({ path: root, workspacesEnabled }, config));
41
+ const actualTree = await arborist.reify({
42
+ add,
43
+ rm: remove,
44
+ saveType: saveAs,
45
+ save: lockFile,
46
+ update: true,
47
+ });
48
+ return [...actualTree.edgesOut.values()].map((edge) => ({
49
+ name: edge.name,
50
+ spec: edge.spec,
51
+ }));
129
52
  }
130
- /**
131
- * get the url of the package registry of the target project
132
- * @returns url of the registry
133
- */
134
-
135
-
53
+ exports.reifyDependencies = reifyDependencies;
136
54
  async function getNPMConfig() {
137
- // get npm config
138
- const config = new _config.default({
139
- definitions: {
140
- userconfig: {
141
- default: (0, _path.resolve)((0, _os.homedir)(), '.npmrc'),
142
- type: String
143
- },
144
- registry: {
145
- default: 'https://registry.npmjs.org',
146
- type: String
147
- }
148
- },
149
- npmPath: '.',
150
- flatten: (config, flattenedConfig) => {
151
- // NOTE: this function is called multiple time for flattening the configs at multiple levels, user, project, cli etc.
152
- // ensure that higher level configs are always in priority
153
- Object.assign(flattenedConfig, config);
154
- }
155
- });
156
- await config.load();
157
- return config.flat;
55
+ const config = new config_1.default({
56
+ definitions: {
57
+ userconfig: { default: (0, path_1.resolve)((0, os_1.homedir)(), '.npmrc'), type: String },
58
+ registry: { default: 'https://registry.npmjs.org', type: String },
59
+ },
60
+ npmPath: '.',
61
+ flatten: (config, flattenedConfig) => {
62
+ Object.assign(flattenedConfig, config);
63
+ },
64
+ });
65
+ await config.load();
66
+ return config.flat;
158
67
  }
159
- //# sourceMappingURL=package.js.map
68
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGFja2FnZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NvdXJjZS9wYWNrYWdlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQWVBLCtDQUE0QztBQUM1Qyw0REFBb0M7QUFDcEMsbUNBQXNDO0FBQ3RDLDJCQUE2QjtBQUM3QiwrQkFBK0I7QUFDL0IsOERBQTRDO0FBSTVDLE1BQU0saUNBQWlDLEdBQUcsQ0FBQyxDQUFDO0FBYzVDLFNBQWdCLDRCQUE0Qjs7SUFJMUMsTUFBTSxDQUFDLFFBQVEsQ0FBQyxHQUNkLE1BQUEsTUFBQSxPQUFPLENBQUMsR0FBRy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SxFQUFFLE9BQU8sRUFBRSxJQUFBLGNBQU8sRUFBQyxJQUFBLFlBQU8sR0FBRSxFQUFFLFFBQVEsQ0FBQyxFQUFFLElBQUksRUFBRSxNQUFNLEVBQUU7WUFDbkUsUUFBUSxFQUFFLEVBQUUsT0FBTyxFQUFFLDRCQUE0QixFQUFFLElBQUksRUFBRSxNQUFNLEVBQUU7U0FDbEU7UUFDRCxPQUFPLEVBQUUsR0FBRztRQUNaLE9BQU8sRUFBRSxDQUNQLE1BQThCLEVBQzlCLGVBQXVDLEVBQ2pDLEVBQUU7WUFJUixNQUFNLENBQUMsTUFBTSxDQUFDLGVBQWUsRUFBRSxNQUFNLENBQUMsQ0FBQztRQUN6QyxDQUFDO0tBQ0YsQ0FBQyxDQUFDO0lBRUgsTUFBTSxNQUFNLENBQUMsSUFBSSxFQUFFLENBQUM7SUFFcEIsT0FBTyxNQUFNLENBQUMsSUFBSSxDQUFDO0FBQ3JCLENBQUMifQ==
package/lib/preset.d.ts CHANGED
@@ -1,13 +1,13 @@
1
- import type { PresetAsset, PresetContext, PresetterConfig, ResolvedPresetContext, Template } from './types';
1
+ import type { PresetAsset, PresetContext, PresetGraph, PresetNode, PresetterConfig, ResolvedPresetContext, Template } from './types';
2
2
  export declare function getPresetterRC(root: string): Promise<PresetterConfig>;
3
3
  export declare function updatePresetterRC(root: string, config: PresetterConfig): Promise<void>;
4
4
  export declare function assertPresetterRC(value: unknown): asserts value is PresetterConfig;
5
- export declare function getPresetAssets(context: PresetContext): Promise<PresetAsset[]>;
6
- export declare function getScripts(context: PresetContext): Promise<Record<string, string>>;
5
+ export declare function getPresetAsset(name: string, context: PresetContext): Promise<PresetAsset>;
6
+ export declare function getPresetNode(name: string, context: PresetContext): Promise<PresetNode>;
7
+ export declare function getPresetGraph(context: PresetContext): Promise<PresetGraph>;
8
+ export declare function getScripts(): Promise<Record<string, string>>;
7
9
  export declare function setupPreset(...uris: string[]): Promise<void>;
8
- export declare function bootstrapPreset(options?: {
9
- force?: boolean;
10
- }): Promise<void>;
10
+ export declare function bootstrapPreset(): Promise<void>;
11
11
  export declare function bootstrapContent(context: PresetContext): Promise<void>;
12
12
  export declare function unsetPreset(): Promise<void>;
13
13
  export declare function getContext(): Promise<PresetContext>;