@neon-rs/manifest 0.0.2 → 0.0.4

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.
@@ -0,0 +1,85 @@
1
+ "use strict";
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]; } };
7
+ }
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.AbstractManifest = exports.readManifest = void 0;
27
+ const fs = __importStar(require("node:fs/promises"));
28
+ const path = __importStar(require("node:path"));
29
+ async function readManifest(dir) {
30
+ dir = dir ?? process.cwd();
31
+ return JSON.parse(await fs.readFile(path.join(dir, "package.json"), { encoding: 'utf8' }));
32
+ }
33
+ exports.readManifest = readManifest;
34
+ function assertIsPreamble(json) {
35
+ if (!json || typeof json !== 'object') {
36
+ throw new TypeError(`expected binary Neon package manifest, found ${json}`);
37
+ }
38
+ if (!('version' in json) || typeof json.version !== 'string') {
39
+ throw new TypeError('valid "version" string not found in Neon package manifest');
40
+ }
41
+ if (!('name' in json) || typeof json.name !== 'string') {
42
+ throw new TypeError('valid "name" string not found in Neon package manifest');
43
+ }
44
+ }
45
+ const OPTIONAL_KEYS = [
46
+ 'author', 'repository', 'keywords', 'bugs', 'homepage', 'license', 'engines'
47
+ ];
48
+ // export type OptionalKeys = {
49
+ // author?: string | undefined,
50
+ // repository?: string | undefined,
51
+ // keywords?: string[] | undefined,
52
+ // bugs?: string | object | undefined,
53
+ // homepage?: string | undefined,
54
+ // license?: string | undefined,
55
+ // engines?: object | undefined
56
+ // };
57
+ class AbstractManifest {
58
+ constructor(json) {
59
+ assertIsPreamble(json);
60
+ this._json = json;
61
+ this._changes = [];
62
+ }
63
+ get name() { return this._json.name; }
64
+ set name(value) { this._json.name = value; }
65
+ get version() { return this._json.version; }
66
+ set version(value) { this._json.version = value; }
67
+ get description() { return this._json.description ?? ""; }
68
+ hasUnsavedChanges() {
69
+ return this._changes.length > 0;
70
+ }
71
+ async save(dir, log) {
72
+ await fs.writeFile(path.join(dir, "package.json"), JSON.stringify(this._json, null, 2), { encoding: 'utf8' });
73
+ }
74
+ stringify() {
75
+ return JSON.stringify(this._json);
76
+ }
77
+ copyOptionalKeys(target) {
78
+ for (const key of OPTIONAL_KEYS) {
79
+ if (key in this._json) {
80
+ target[key] = this._json[key];
81
+ }
82
+ }
83
+ }
84
+ }
85
+ exports.AbstractManifest = AbstractManifest;
@@ -0,0 +1,25 @@
1
+ import { Change } from './change.cjs';
2
+ export declare function readManifest(dir?: string | undefined): Promise<unknown>;
3
+ type Preamble = {
4
+ name: string;
5
+ version: string;
6
+ optionalDependencies?: Record<string, string> | undefined;
7
+ [key: string]: any;
8
+ };
9
+ export declare class AbstractManifest implements Preamble {
10
+ protected _json: Preamble;
11
+ protected _changes: Change[];
12
+ constructor(json: unknown);
13
+ get name(): string;
14
+ set name(value: string);
15
+ get version(): string;
16
+ set version(value: string);
17
+ get description(): string;
18
+ hasUnsavedChanges(): boolean;
19
+ save(dir: string, log: (msg: string) => void): Promise<void>;
20
+ stringify(): string;
21
+ copyOptionalKeys(target: {
22
+ [key: string]: unknown;
23
+ }): void;
24
+ }
25
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,9 @@
1
+ import { NodePlatform, RustTarget } from '../platform.cjs';
2
+ import { LibraryManifest } from '../index.cjs';
3
+ export interface CacheCfg {
4
+ readonly type: string;
5
+ hasUnsavedChanges(): boolean;
6
+ setPlatformTarget(platform: NodePlatform, target: RustTarget): Promise<void>;
7
+ updatePlatforms(lib: LibraryManifest): boolean;
8
+ saveChanges(log: (msg: string) => void): Promise<void>;
9
+ }
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeBinaryCfg = void 0;
4
+ const platform_cjs_1 = require("../../platform.cjs");
5
+ const util_cjs_1 = require("../../util.cjs");
6
+ const neon_cjs_1 = require("../../library/neon.cjs");
7
+ function assertIsBinaryV1(json) {
8
+ (0, util_cjs_1.assertHasProps)(['binary'], json, "neon");
9
+ const binary = json.binary;
10
+ if (!binary || typeof binary !== 'object') {
11
+ throw new TypeError(`expected "neon.binary" to be an object, found ${binary}`);
12
+ }
13
+ (0, util_cjs_1.assertHasProps)(['rust', 'node', 'platform', 'arch', 'abi'], binary, "neon.binary");
14
+ if (typeof binary.rust !== 'string' || !(0, platform_cjs_1.isRustTarget)(binary.rust)) {
15
+ throw new TypeError(`expected "neon.binary.rust" to be a valid Rust target, found ${binary.rust}`);
16
+ }
17
+ if (!(0, platform_cjs_1.isNodePlatform)(binary.node)) {
18
+ throw new TypeError(`expected "neon.binary.node" to be a valid Node platform, found ${binary.node}`);
19
+ }
20
+ if (typeof binary.platform !== 'string') {
21
+ throw new TypeError(`expected "neon.binary.platform" to be a string, found ${binary.platform}`);
22
+ }
23
+ if (typeof binary.arch !== 'string') {
24
+ throw new TypeError(`expected "neon.binary.arch" to be a string, found ${binary.arch}`);
25
+ }
26
+ if (binary.abi !== null && typeof binary.abi !== 'string') {
27
+ throw new TypeError(`expected "neon.binary.abi" to be a string or null, found ${binary.abi}`);
28
+ }
29
+ }
30
+ function assertIsBinaryV2(json) {
31
+ if (!json || typeof json !== 'object') {
32
+ throw new TypeError(`expected "neon" to be an object, found ${json}`);
33
+ }
34
+ (0, util_cjs_1.assertHasProps)(['rust', 'node', 'platform', 'arch', 'abi'], json, "neon");
35
+ if (!(0, platform_cjs_1.isRustTarget)(json.rust)) {
36
+ throw new TypeError(`expected "neon.rust" to be a valid Rust target, found ${json.rust}`);
37
+ }
38
+ if (!(0, platform_cjs_1.isNodePlatform)(json.node)) {
39
+ throw new TypeError(`expected "neon.node" to be a valid Node platform, found ${json.node}`);
40
+ }
41
+ if (typeof json.platform !== 'string') {
42
+ throw new TypeError(`expected "neon.platform" to be a string, found ${json.platform}`);
43
+ }
44
+ if (typeof json.arch !== 'string') {
45
+ throw new TypeError(`expected "neon.arch" to be a string, found ${json.arch}`);
46
+ }
47
+ if (json.abi !== null && typeof json.abi !== 'string') {
48
+ throw new TypeError(`expected "neon.abi" to be a string or null, found ${json.abi}`);
49
+ }
50
+ }
51
+ function normalizeBinaryCfg(json) {
52
+ (0, neon_cjs_1.assertHasNeonCfg)(json);
53
+ // V3 format: {
54
+ // neon: {
55
+ // type: 'binary',
56
+ // rust: RustTarget,
57
+ // node: NodeTarget,
58
+ // os: string,
59
+ // arch: string,
60
+ // abi: string | null
61
+ // }
62
+ // }
63
+ if ('type' in json.neon && 'os' in json.neon) {
64
+ return false;
65
+ }
66
+ // V2 format: {
67
+ // neon: {
68
+ // type: 'binary',
69
+ // rust: RustTarget,
70
+ // node: NodeTarget,
71
+ // platform: string,
72
+ // arch: string,
73
+ // abi: string | null
74
+ // }
75
+ // }
76
+ if ('type' in json.neon) {
77
+ json.neon = upgradeBinaryV2(json.neon);
78
+ return true;
79
+ }
80
+ // V1 format: {
81
+ // neon: {
82
+ // binary: {
83
+ // rust: RustTarget,
84
+ // node: NodeTarget,
85
+ // platform: string,
86
+ // arch: string,
87
+ // abi: string | null
88
+ // }
89
+ // }
90
+ // }
91
+ json.neon = upgradeBinaryV1(json.neon);
92
+ return true;
93
+ }
94
+ exports.normalizeBinaryCfg = normalizeBinaryCfg;
95
+ function upgradeBinaryV1(json) {
96
+ assertIsBinaryV1(json);
97
+ return {
98
+ type: 'binary',
99
+ rust: json.binary.rust,
100
+ node: json.binary.node,
101
+ os: json.binary.platform,
102
+ arch: json.binary.arch,
103
+ abi: json.binary.abi
104
+ };
105
+ }
106
+ function upgradeBinaryV2(json) {
107
+ assertIsBinaryV2(json);
108
+ return {
109
+ type: 'binary',
110
+ rust: json.rust,
111
+ node: json.node,
112
+ os: json.platform,
113
+ arch: json.arch,
114
+ abi: json.abi
115
+ };
116
+ }
@@ -0,0 +1 @@
1
+ export declare function normalizeBinaryCfg(json: object): boolean;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BinaryManifest = exports.SCHEMA_VERSION = void 0;
4
+ const platform_cjs_1 = require("../../platform.cjs");
5
+ const neon_cjs_1 = require("../../library/neon.cjs");
6
+ const util_cjs_1 = require("../../util.cjs");
7
+ const legacy_cjs_1 = require("./legacy.cjs");
8
+ exports.SCHEMA_VERSION = 3;
9
+ function assertIsBinaryCfg(json) {
10
+ (0, util_cjs_1.assertHasProps)(['type', 'rust', 'node', 'os', 'arch', 'abi'], json, "neon");
11
+ if (json.type !== 'binary') {
12
+ throw new TypeError(`expected "neon.type" property to be "binary", found ${json.type}`);
13
+ }
14
+ if (typeof json.rust !== 'string' || !(0, platform_cjs_1.isRustTarget)(json.rust)) {
15
+ throw new TypeError(`expected "neon.rust" to be a valid Rust target, found ${json.rust}`);
16
+ }
17
+ if (typeof json.node !== 'string' || !(0, platform_cjs_1.isNodePlatform)(json.node)) {
18
+ throw new TypeError(`expected "neon.node" to be a valid Node target, found ${json.node}`);
19
+ }
20
+ if (typeof json.os !== 'string') {
21
+ throw new TypeError(`expected "neon.os" to be a string, found ${json.os}`);
22
+ }
23
+ if (typeof json.arch !== 'string') {
24
+ throw new TypeError(`expected "neon.arch" to be a string, found ${json.arch}`);
25
+ }
26
+ if (json.abi !== null && typeof json.abi !== 'string') {
27
+ throw new TypeError(`expected "neon.abi" to be a string or null, found ${json.abi}`);
28
+ }
29
+ }
30
+ function assertHasBinaryCfg(json) {
31
+ (0, neon_cjs_1.assertHasNeonCfg)(json);
32
+ assertIsBinaryCfg(json.neon);
33
+ }
34
+ class BinaryManifest extends util_cjs_1.AbstractManifest {
35
+ constructor(dir, json, isNew) {
36
+ super(json);
37
+ this.dir = dir;
38
+ this._schemaUpgraded = (0, legacy_cjs_1.normalizeBinaryCfg)(this._json);
39
+ this._targetChanged = false;
40
+ assertHasBinaryCfg(this._json);
41
+ this._binaryJSON = this._json;
42
+ this._new = isNew;
43
+ }
44
+ get isNew() {
45
+ return this._new;
46
+ }
47
+ get schemaUpgraded() {
48
+ return this._schemaUpgraded;
49
+ }
50
+ get targetChanged() {
51
+ return this._targetChanged;
52
+ }
53
+ cfg() {
54
+ return this._binaryJSON.neon;
55
+ }
56
+ setTarget(target) {
57
+ const targetInfo = (0, platform_cjs_1.describeTarget)(target);
58
+ this._json.os = targetInfo.os;
59
+ this._json.cpu = targetInfo.arch;
60
+ this._binaryJSON.neon.rust = target;
61
+ this._binaryJSON.neon.os = targetInfo.os;
62
+ this._binaryJSON.neon.arch = targetInfo.arch;
63
+ this._binaryJSON.neon.abi = targetInfo.abi;
64
+ this._targetChanged = true;
65
+ }
66
+ hasUnsavedChanges() {
67
+ return this._new || this._schemaUpgraded || this._targetChanged;
68
+ }
69
+ async save(log) {
70
+ await super.save(log);
71
+ this._new = false;
72
+ this._schemaUpgraded = false;
73
+ this._targetChanged = false;
74
+ }
75
+ }
76
+ exports.BinaryManifest = BinaryManifest;
@@ -0,0 +1,26 @@
1
+ import { RustTarget, NodePlatform } from '../../platform.cjs';
2
+ import { AbstractManifest, Preamble } from '../../util.cjs';
3
+ export declare const SCHEMA_VERSION = 3;
4
+ export interface BinaryCfg {
5
+ type: "binary";
6
+ rust: RustTarget;
7
+ node: NodePlatform;
8
+ os: string;
9
+ arch: string;
10
+ abi: string | null;
11
+ }
12
+ export declare class BinaryManifest extends AbstractManifest {
13
+ private _binaryJSON;
14
+ readonly dir: string;
15
+ private _schemaUpgraded;
16
+ private _targetChanged;
17
+ private _new;
18
+ constructor(dir: string, json: Preamble, isNew: boolean);
19
+ get isNew(): boolean;
20
+ get schemaUpgraded(): boolean;
21
+ get targetChanged(): boolean;
22
+ cfg(): BinaryCfg;
23
+ setTarget(target: RustTarget): void;
24
+ hasUnsavedChanges(): boolean;
25
+ save(log: (msg: string) => void): Promise<void>;
26
+ }
@@ -0,0 +1,136 @@
1
+ "use strict";
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]; } };
7
+ }
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.NPMCacheCfg = void 0;
30
+ const fs = __importStar(require("node:fs/promises"));
31
+ const path = __importStar(require("node:path"));
32
+ const jscodeshift_1 = __importDefault(require("jscodeshift"));
33
+ const platform_cjs_1 = require("../../platform.cjs");
34
+ const package_cjs_1 = require("./package.cjs");
35
+ const PLATFORMS_DIR = 'platforms';
36
+ class NPMCacheCfg {
37
+ constructor(manifest, outDir = PLATFORMS_DIR) {
38
+ this.type = 'npm';
39
+ this.manifest = manifest;
40
+ this.dir = path.join(manifest.dir, outDir);
41
+ const packages = Object.create(null);
42
+ const platforms = manifest.allPlatforms();
43
+ for (const key in platforms) {
44
+ const node = key;
45
+ const rust = platforms[node];
46
+ packages[node] = package_cjs_1.BinaryPackage.defer(this, node, rust);
47
+ }
48
+ this._packages = packages;
49
+ }
50
+ async setPlatformTarget(platform, target) {
51
+ const pkg = this._packages[platform];
52
+ if (!pkg) {
53
+ this._packages[platform] = package_cjs_1.BinaryPackage.create(this, platform, target);
54
+ }
55
+ else {
56
+ await pkg.setTarget(target);
57
+ }
58
+ }
59
+ hasUnsavedChanges() {
60
+ for (const key in this._packages) {
61
+ const pkg = this._packages[key];
62
+ if (pkg.hasUnsavedChanges()) {
63
+ return true;
64
+ }
65
+ }
66
+ return false;
67
+ }
68
+ newPlatforms() {
69
+ const result = [];
70
+ for (const node in this._packages) {
71
+ if (this._packages[node].isNew()) {
72
+ result.push(node);
73
+ }
74
+ }
75
+ return result;
76
+ }
77
+ async saveChanges(log) {
78
+ const newPlatforms = this.newPlatforms();
79
+ for (const node in this._packages) {
80
+ const pkg = this._packages[node];
81
+ if (pkg.hasUnsavedChanges()) {
82
+ await pkg.saveChanges(log);
83
+ }
84
+ }
85
+ if (newPlatforms.length > 0) {
86
+ await this.updateLoader(newPlatforms);
87
+ }
88
+ }
89
+ async updateLoader(platforms) {
90
+ const cfg = this.manifest.cfg();
91
+ if (!cfg.load) {
92
+ return;
93
+ }
94
+ const loaderPath = path.join(this.manifest.dir, cfg.load);
95
+ const loader = await fs.readFile(loaderPath, 'utf8');
96
+ function isPlatformTable(p) {
97
+ return p.value.properties.every(p => {
98
+ return p.type === 'Property' &&
99
+ p.key.type === 'Literal' &&
100
+ (0, platform_cjs_1.isNodePlatform)(p.key.value);
101
+ });
102
+ }
103
+ const result = (0, jscodeshift_1.default)(loader)
104
+ .find(jscodeshift_1.default.ObjectExpression)
105
+ .filter(isPlatformTable)
106
+ .replaceWith((p) => {
107
+ const newProps = platforms.map(platform => {
108
+ return jscodeshift_1.default.property('init', jscodeshift_1.default.literal(platform), jscodeshift_1.default.arrowFunctionExpression([], jscodeshift_1.default.callExpression(jscodeshift_1.default.identifier('require'), [jscodeshift_1.default.literal(`${cfg.org}/${platform}`)])));
109
+ });
110
+ return jscodeshift_1.default.objectExpression([...p.value.properties, ...newProps]);
111
+ })
112
+ .toSource({ quote: 'single' });
113
+ await fs.writeFile(loaderPath, result, 'utf8');
114
+ }
115
+ packageNames() {
116
+ const cfg = this.manifest.cfg();
117
+ return Object.keys(this.manifest.allPlatforms()).map(key => `${cfg.org}/${key}`);
118
+ }
119
+ updatePlatforms(lib) {
120
+ let changed = false;
121
+ const preamble = lib.preamble;
122
+ if (!preamble.optionalDependencies) {
123
+ preamble.optionalDependencies = {};
124
+ changed = true;
125
+ }
126
+ const packages = this.packageNames();
127
+ for (const pkg of packages) {
128
+ if (preamble.optionalDependencies[pkg] !== lib.version) {
129
+ preamble.optionalDependencies[pkg] = lib.version;
130
+ changed = true;
131
+ }
132
+ }
133
+ return changed;
134
+ }
135
+ }
136
+ exports.NPMCacheCfg = NPMCacheCfg;
@@ -0,0 +1,17 @@
1
+ import { CacheCfg } from '../cache.cjs';
2
+ import { LibraryManifest } from '../../library/library.cjs';
3
+ import { RustTarget, NodePlatform } from '../../platform.cjs';
4
+ export declare class NPMCacheCfg implements CacheCfg {
5
+ readonly type: string;
6
+ readonly dir: string;
7
+ readonly manifest: LibraryManifest;
8
+ private _packages;
9
+ constructor(manifest: LibraryManifest, outDir?: string);
10
+ setPlatformTarget(platform: NodePlatform, target: RustTarget): Promise<void>;
11
+ hasUnsavedChanges(): boolean;
12
+ private newPlatforms;
13
+ saveChanges(log: (msg: string) => void): Promise<void>;
14
+ updateLoader(platforms: NodePlatform[]): Promise<void>;
15
+ packageNames(): string[];
16
+ updatePlatforms(lib: LibraryManifest): boolean;
17
+ }
@@ -0,0 +1,114 @@
1
+ "use strict";
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]; } };
7
+ }
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.BinaryPackage = void 0;
27
+ const platform_cjs_1 = require("../../platform.cjs");
28
+ const manifest_cjs_1 = require("./manifest.cjs");
29
+ const util_cjs_1 = require("../../util.cjs");
30
+ const path = __importStar(require("node:path"));
31
+ const fs = __importStar(require("node:fs/promises"));
32
+ async function loadManifest(cacheCfg, node, rust) {
33
+ const dir = path.join(cacheCfg.dir, node);
34
+ const json = await (0, util_cjs_1.readManifest)(dir);
35
+ return new manifest_cjs_1.BinaryManifest(dir, json, false);
36
+ }
37
+ class BinaryPackage {
38
+ constructor(cacheCfg, node, rust, manifest) {
39
+ this._cacheCfg = cacheCfg;
40
+ this._node = node;
41
+ this._rust = rust;
42
+ this._manifest = manifest;
43
+ }
44
+ async manifest() {
45
+ if (!this._manifest) {
46
+ this._manifest = await loadManifest(this._cacheCfg, this._node, this._rust);
47
+ }
48
+ return this._manifest;
49
+ }
50
+ isNew() {
51
+ return !!(this._manifest && this._manifest.isNew);
52
+ }
53
+ schemaUpgraded() {
54
+ return !!(this._manifest && this._manifest.schemaUpgraded);
55
+ }
56
+ targetChanged() {
57
+ return !!(this._manifest && this._manifest.targetChanged);
58
+ }
59
+ hasUnsavedChanges() {
60
+ return this.isNew() || this.schemaUpgraded() || this.targetChanged();
61
+ }
62
+ async saveChanges(log) {
63
+ const manifest = await this.manifest();
64
+ if (this.isNew()) {
65
+ log(`prebuild manifest: ${manifest.stringify()}`);
66
+ log(`creating ${manifest.dir}`);
67
+ await fs.mkdir(manifest.dir, { recursive: true });
68
+ log(`created ${manifest.dir}`);
69
+ log(`creating ${manifest.dir}/README.md`);
70
+ await fs.writeFile(path.join(manifest.dir, "README.md"), `# \`${manifest.name}\`\n\n${manifest.description}\n`);
71
+ log(`creating ${manifest.dir}/package.json`);
72
+ await manifest.save(log);
73
+ }
74
+ else if (manifest.hasUnsavedChanges()) {
75
+ log(`saved changes to ${manifest.dir}/package.json`);
76
+ await manifest.save(log);
77
+ }
78
+ }
79
+ async setTarget(target) {
80
+ (await this.manifest()).setTarget(target);
81
+ }
82
+ // Lazily load a package. The manifest will actually be read from
83
+ // disk via this.manifest() the first time it's invoked.
84
+ static defer(cacheCfg, node, rust) {
85
+ return new BinaryPackage(cacheCfg, node, rust, null);
86
+ }
87
+ static create(cacheCfg, node, rust) {
88
+ const targetInfo = (0, platform_cjs_1.describeTarget)(rust);
89
+ const libraryManifest = cacheCfg.manifest;
90
+ const org = libraryManifest.cfg().org;
91
+ const name = `${org}/${node}`;
92
+ const json = {
93
+ name,
94
+ description: `Prebuilt binary package for \`${libraryManifest.name}\` on \`${targetInfo.node}\`.`,
95
+ version: libraryManifest.version,
96
+ os: [targetInfo.os],
97
+ cpu: [targetInfo.arch],
98
+ main: "index.node",
99
+ files: ["index.node"],
100
+ neon: {
101
+ type: "binary",
102
+ rust: rust,
103
+ node: targetInfo.node,
104
+ os: targetInfo.os,
105
+ arch: targetInfo.arch,
106
+ abi: targetInfo.abi
107
+ }
108
+ };
109
+ libraryManifest.copyOptionalKeys(json);
110
+ const binaryManifest = new manifest_cjs_1.BinaryManifest(path.join(cacheCfg.dir, node), json, true);
111
+ return new BinaryPackage(cacheCfg, node, rust, binaryManifest);
112
+ }
113
+ }
114
+ exports.BinaryPackage = BinaryPackage;
@@ -0,0 +1,19 @@
1
+ import { NPMCacheCfg } from "./npm.cjs";
2
+ import { RustTarget, NodePlatform } from "../../platform.cjs";
3
+ import { BinaryManifest } from "./manifest.cjs";
4
+ export declare class BinaryPackage {
5
+ private _cacheCfg;
6
+ private _node;
7
+ private _rust;
8
+ private _manifest;
9
+ constructor(cacheCfg: NPMCacheCfg, node: NodePlatform, rust: RustTarget, manifest: BinaryManifest | null);
10
+ manifest(): Promise<BinaryManifest>;
11
+ isNew(): boolean;
12
+ schemaUpgraded(): boolean;
13
+ targetChanged(): boolean;
14
+ hasUnsavedChanges(): boolean;
15
+ saveChanges(log: (msg: string) => void): Promise<void>;
16
+ setTarget(target: RustTarget): Promise<void>;
17
+ static defer(cacheCfg: NPMCacheCfg, node: NodePlatform, rust: RustTarget): BinaryPackage;
18
+ static create(cacheCfg: NPMCacheCfg, node: NodePlatform, rust: RustTarget): BinaryPackage;
19
+ }