@contrail/flexplm 1.3.2-alpha.30ca8bf → 1.3.2-alpha.3ffe557

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.
Files changed (113) hide show
  1. package/lib/cli/commands/compile.d.ts +4 -0
  2. package/lib/cli/commands/compile.js +73 -0
  3. package/lib/cli/commands/compile.spec.d.ts +1 -0
  4. package/lib/cli/commands/compile.spec.js +80 -0
  5. package/lib/cli/commands/create.d.ts +5 -0
  6. package/lib/cli/commands/create.js +77 -0
  7. package/lib/cli/commands/create.spec.d.ts +1 -0
  8. package/lib/cli/commands/create.spec.js +78 -0
  9. package/lib/cli/commands/upload.d.ts +17 -0
  10. package/lib/cli/commands/upload.js +228 -0
  11. package/lib/cli/commands/upload.spec.d.ts +1 -0
  12. package/lib/cli/commands/upload.spec.js +88 -0
  13. package/lib/cli/index.d.ts +5 -0
  14. package/lib/cli/index.js +70 -0
  15. package/lib/cli/index.spec.d.ts +1 -0
  16. package/lib/cli/index.spec.js +85 -0
  17. package/lib/cli/template/mapping-template.ts.template +62 -0
  18. package/lib/entity-processor/base-entity-processor.d.ts +47 -10
  19. package/lib/entity-processor/base-entity-processor.js +53 -34
  20. package/lib/entity-processor/base-entity-processor.spec.js +1 -191
  21. package/lib/index.d.ts +1 -0
  22. package/lib/index.js +1 -0
  23. package/lib/interfaces/mapping-file.d.ts +460 -0
  24. package/lib/interfaces/mapping-file.js +2 -0
  25. package/lib/publish/base-process-publish-assortment.d.ts +25 -0
  26. package/lib/publish/base-process-publish-assortment.js +60 -6
  27. package/lib/publish/base-process-publish-assortment.spec.js +22 -4
  28. package/lib/publish/mockData.js +5 -0
  29. package/lib/transform/identifier-conversion-spec-mockData.js +34 -6
  30. package/lib/transform/identifier-conversion.d.ts +36 -0
  31. package/lib/transform/identifier-conversion.js +36 -0
  32. package/lib/transform/identifier-conversion.spec.js +4 -0
  33. package/lib/util/config-defaults.d.ts +0 -1
  34. package/lib/util/config-defaults.js +3 -3
  35. package/lib/util/config-defaults.spec.js +9 -32
  36. package/lib/util/data-converter-spec-mockData.js +17 -3
  37. package/lib/util/data-converter.d.ts +97 -0
  38. package/lib/util/data-converter.js +133 -1
  39. package/lib/util/data-converter.spec.js +68 -0
  40. package/lib/util/error-response-object.d.ts +5 -0
  41. package/lib/util/error-response-object.js +7 -0
  42. package/lib/util/event-short-message-status.js +1 -0
  43. package/lib/util/federation.js +8 -0
  44. package/lib/util/flexplm-connect.d.ts +7 -0
  45. package/lib/util/flexplm-connect.js +14 -0
  46. package/lib/util/logger-config.js +1 -0
  47. package/lib/util/map-util-spec-mockData.js +17 -3
  48. package/lib/util/map-utils.d.ts +27 -0
  49. package/lib/util/map-utils.js +27 -0
  50. package/lib/util/thumbnail-util.d.ts +21 -0
  51. package/lib/util/thumbnail-util.js +28 -1
  52. package/lib/util/thumbnail-util.spec.js +6 -0
  53. package/lib/util/type-conversion-utils-spec-mockData.js +3 -4
  54. package/lib/util/type-conversion-utils.d.ts +140 -1
  55. package/lib/util/type-conversion-utils.js +143 -16
  56. package/lib/util/type-conversion-utils.spec.js +0 -59
  57. package/lib/util/type-defaults.d.ts +58 -0
  58. package/lib/util/type-defaults.js +58 -0
  59. package/lib/util/type-defaults.spec.js +5 -5
  60. package/lib/util/type-utils.d.ts +21 -0
  61. package/lib/util/type-utils.js +23 -0
  62. package/lib/util/type-utils.spec.js +2 -0
  63. package/package.json +21 -6
  64. package/scripts/copy-template.js +10 -0
  65. package/.github/pull_request_template.md +0 -31
  66. package/.github/workflows/flexplm-lib.yml +0 -27
  67. package/.github/workflows/publish-to-npm.yml +0 -121
  68. package/CHANGELOG.md +0 -36
  69. package/publish.bat +0 -5
  70. package/publish.sh +0 -5
  71. package/src/entity-processor/base-entity-processor.spec.ts +0 -689
  72. package/src/entity-processor/base-entity-processor.ts +0 -583
  73. package/src/flexplm-request.ts +0 -28
  74. package/src/flexplm-utils.spec.ts +0 -27
  75. package/src/flexplm-utils.ts +0 -29
  76. package/src/index.ts +0 -22
  77. package/src/interfaces/interfaces.ts +0 -122
  78. package/src/interfaces/item-family-changes.ts +0 -67
  79. package/src/interfaces/publish-change-data.ts +0 -43
  80. package/src/publish/base-process-publish-assortment-callback.ts +0 -50
  81. package/src/publish/base-process-publish-assortment.spec.ts +0 -1992
  82. package/src/publish/base-process-publish-assortment.ts +0 -1134
  83. package/src/publish/mockData.ts +0 -4561
  84. package/src/transform/identifier-conversion-spec-mockData.ts +0 -496
  85. package/src/transform/identifier-conversion.spec.ts +0 -354
  86. package/src/transform/identifier-conversion.ts +0 -282
  87. package/src/util/config-defaults.spec.ts +0 -392
  88. package/src/util/config-defaults.ts +0 -97
  89. package/src/util/data-converter-spec-mockData.ts +0 -231
  90. package/src/util/data-converter.spec.ts +0 -1041
  91. package/src/util/data-converter.ts +0 -762
  92. package/src/util/error-response-object.spec.ts +0 -116
  93. package/src/util/error-response-object.ts +0 -50
  94. package/src/util/event-short-message-status.ts +0 -22
  95. package/src/util/federation.ts +0 -172
  96. package/src/util/flexplm-connect.spec.ts +0 -132
  97. package/src/util/flexplm-connect.ts +0 -208
  98. package/src/util/logger-config.ts +0 -20
  99. package/src/util/map-util-spec-mockData.ts +0 -231
  100. package/src/util/map-utils.spec.ts +0 -103
  101. package/src/util/map-utils.ts +0 -41
  102. package/src/util/mockData.ts +0 -101
  103. package/src/util/thumbnail-util.spec.ts +0 -508
  104. package/src/util/thumbnail-util.ts +0 -272
  105. package/src/util/type-conversion-utils-spec-mockData.ts +0 -272
  106. package/src/util/type-conversion-utils.spec.ts +0 -1031
  107. package/src/util/type-conversion-utils.ts +0 -490
  108. package/src/util/type-defaults.spec.ts +0 -669
  109. package/src/util/type-defaults.ts +0 -281
  110. package/src/util/type-utils.spec.ts +0 -227
  111. package/src/util/type-utils.ts +0 -144
  112. package/tsconfig.json +0 -24
  113. package/tslint.json +0 -57
@@ -0,0 +1,4 @@
1
+ export declare class CompileCommand {
2
+ private loadTypescript;
3
+ run(tsPath: string): Promise<void>;
4
+ }
@@ -0,0 +1,73 @@
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.CompileCommand = void 0;
27
+ const fs = __importStar(require("fs"));
28
+ const path = __importStar(require("path"));
29
+ class CompileCommand {
30
+ loadTypescript() {
31
+ try {
32
+ return require('typescript');
33
+ }
34
+ catch (e) {
35
+ throw new Error('compile requires the "typescript" package. Install it in your project: npm install --save-dev typescript');
36
+ }
37
+ }
38
+ async run(tsPath) {
39
+ const absPath = path.resolve(process.cwd(), tsPath);
40
+ if (!fs.existsSync(absPath)) {
41
+ throw new Error(`File not found: ${absPath}`);
42
+ }
43
+ if (!absPath.endsWith('.ts')) {
44
+ throw new Error(`Expected a .ts file, got: ${absPath}`);
45
+ }
46
+ const ts = this.loadTypescript();
47
+ const source = fs.readFileSync(absPath, 'utf8');
48
+ const result = ts.transpileModule(source, {
49
+ fileName: path.basename(absPath),
50
+ compilerOptions: {
51
+ module: ts.ModuleKind.CommonJS,
52
+ target: ts.ScriptTarget.ES2020,
53
+ esModuleInterop: true,
54
+ },
55
+ reportDiagnostics: true,
56
+ });
57
+ const diagnostics = result.diagnostics || [];
58
+ if (diagnostics.length > 0) {
59
+ const formatHost = {
60
+ getCanonicalFileName: (f) => f,
61
+ getCurrentDirectory: () => process.cwd(),
62
+ getNewLine: () => '\n',
63
+ };
64
+ const formatted = ts.formatDiagnosticsWithColorAndContext(diagnostics, formatHost);
65
+ process.stderr.write(formatted);
66
+ throw new Error(`Compile failed: ${diagnostics.length} diagnostic(s)`);
67
+ }
68
+ const outPath = absPath.replace(/\.ts$/, '.js');
69
+ fs.writeFileSync(outPath, result.outputText, 'utf8');
70
+ console.log(`Compiled ${outPath}`);
71
+ }
72
+ }
73
+ exports.CompileCommand = CompileCommand;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,80 @@
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
+ const fs = __importStar(require("fs"));
27
+ const os = __importStar(require("os"));
28
+ const path = __importStar(require("path"));
29
+ const compile_1 = require("./compile");
30
+ describe('runCompile', () => {
31
+ let tempDir;
32
+ let originalCwd;
33
+ let logSpy;
34
+ let stderrSpy;
35
+ beforeEach(() => {
36
+ originalCwd = process.cwd();
37
+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'flexplm-compile-'));
38
+ process.chdir(tempDir);
39
+ logSpy = jest.spyOn(console, 'log').mockImplementation(() => { });
40
+ stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => true);
41
+ });
42
+ afterEach(() => {
43
+ logSpy.mockRestore();
44
+ stderrSpy.mockRestore();
45
+ process.chdir(originalCwd);
46
+ fs.rmSync(tempDir, { recursive: true, force: true });
47
+ });
48
+ it('transpiles a .ts file to a sibling .js file', async () => {
49
+ const tsPath = path.join(tempDir, 'mapping.ts');
50
+ fs.writeFileSync(tsPath, `export const mapping = { orgName: 'acme' };\n`, 'utf8');
51
+ await new compile_1.CompileCommand().run('mapping.ts');
52
+ const jsPath = path.join(tempDir, 'mapping.js');
53
+ expect(fs.existsSync(jsPath)).toBe(true);
54
+ const js = fs.readFileSync(jsPath, 'utf8');
55
+ expect(js).toContain('exports.mapping');
56
+ expect(js).toContain("orgName: 'acme'");
57
+ });
58
+ it('resolves the input path relative to cwd', async () => {
59
+ const subdir = path.join(tempDir, 'nested');
60
+ fs.mkdirSync(subdir);
61
+ const tsPath = path.join(subdir, 'm.ts');
62
+ fs.writeFileSync(tsPath, 'export const x = 1;\n', 'utf8');
63
+ await new compile_1.CompileCommand().run('nested/m.ts');
64
+ expect(fs.existsSync(path.join(subdir, 'm.js'))).toBe(true);
65
+ });
66
+ it('throws when the input file does not exist', async () => {
67
+ await expect(new compile_1.CompileCommand().run('nope.ts')).rejects.toThrow(/File not found/);
68
+ });
69
+ it('throws when the input is not a .ts file', async () => {
70
+ const jsPath = path.join(tempDir, 'mapping.js');
71
+ fs.writeFileSync(jsPath, 'module.exports = {};', 'utf8');
72
+ await expect(new compile_1.CompileCommand().run('mapping.js')).rejects.toThrow(/Expected a \.ts file/);
73
+ });
74
+ it('throws on a TypeScript syntax error', async () => {
75
+ const tsPath = path.join(tempDir, 'broken.ts');
76
+ fs.writeFileSync(tsPath, 'export const = ;\n', 'utf8');
77
+ await expect(new compile_1.CompileCommand().run('broken.ts')).rejects.toThrow(/Compile failed/);
78
+ expect(fs.existsSync(path.join(tempDir, 'broken.js'))).toBe(false);
79
+ });
80
+ });
@@ -0,0 +1,5 @@
1
+ export declare class CreateCommand {
2
+ private prompt;
3
+ private findTemplate;
4
+ run(): Promise<void>;
5
+ }
@@ -0,0 +1,77 @@
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.CreateCommand = void 0;
27
+ const fs = __importStar(require("fs"));
28
+ const path = __importStar(require("path"));
29
+ const readline = __importStar(require("readline"));
30
+ const TEMPLATE_FILENAME = 'mapping-template.ts.template';
31
+ const PLACEHOLDER = '<ORG_NAME>';
32
+ class CreateCommand {
33
+ prompt(question) {
34
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
35
+ const onSigint = () => {
36
+ rl.close();
37
+ process.stdout.write('\n');
38
+ process.exit(130);
39
+ };
40
+ process.once('SIGINT', onSigint);
41
+ return new Promise((resolve) => {
42
+ rl.question(question, (answer) => {
43
+ process.removeListener('SIGINT', onSigint);
44
+ rl.close();
45
+ resolve(answer);
46
+ });
47
+ });
48
+ }
49
+ findTemplate() {
50
+ const candidates = [
51
+ path.join(__dirname, '..', 'template', TEMPLATE_FILENAME),
52
+ path.join(__dirname, '..', '..', '..', 'src', 'cli', 'template', TEMPLATE_FILENAME),
53
+ ];
54
+ for (const candidate of candidates) {
55
+ if (fs.existsSync(candidate)) {
56
+ return candidate;
57
+ }
58
+ }
59
+ throw new Error(`Could not locate ${TEMPLATE_FILENAME}. Tried:\n ${candidates.join('\n ')}`);
60
+ }
61
+ async run() {
62
+ const orgName = (await this.prompt('orgName: ')).trim();
63
+ if (!orgName) {
64
+ throw new Error('orgName is required');
65
+ }
66
+ const templatePath = this.findTemplate();
67
+ const templateBody = fs.readFileSync(templatePath, 'utf8');
68
+ const rendered = templateBody.split(PLACEHOLDER).join(orgName);
69
+ const outPath = path.resolve(process.cwd(), `${orgName}-transformMapping.ts`);
70
+ if (fs.existsSync(outPath)) {
71
+ throw new Error(`Refusing to overwrite existing file: ${outPath}`);
72
+ }
73
+ fs.writeFileSync(outPath, rendered, 'utf8');
74
+ console.log(`Created ${outPath}`);
75
+ }
76
+ }
77
+ exports.CreateCommand = CreateCommand;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,78 @@
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
+ const fs = __importStar(require("fs"));
27
+ const os = __importStar(require("os"));
28
+ const path = __importStar(require("path"));
29
+ let nextAnswer = '';
30
+ jest.mock('readline', () => ({
31
+ createInterface: () => ({
32
+ question: (_q, cb) => cb(nextAnswer),
33
+ close: () => { },
34
+ }),
35
+ }));
36
+ const create_1 = require("./create");
37
+ describe('runCreate', () => {
38
+ let tempDir;
39
+ let originalCwd;
40
+ let logSpy;
41
+ beforeEach(() => {
42
+ originalCwd = process.cwd();
43
+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'flexplm-create-'));
44
+ process.chdir(tempDir);
45
+ logSpy = jest.spyOn(console, 'log').mockImplementation(() => { });
46
+ nextAnswer = '';
47
+ });
48
+ afterEach(() => {
49
+ logSpy.mockRestore();
50
+ process.chdir(originalCwd);
51
+ fs.rmSync(tempDir, { recursive: true, force: true });
52
+ });
53
+ it('writes <orgName>-transformMapping.ts with the placeholder replaced', async () => {
54
+ nextAnswer = 'acme';
55
+ await new create_1.CreateCommand().run();
56
+ const outPath = path.join(tempDir, 'acme-transformMapping.ts');
57
+ expect(fs.existsSync(outPath)).toBe(true);
58
+ const contents = fs.readFileSync(outPath, 'utf8');
59
+ expect(contents).toContain("orgName: 'acme'");
60
+ expect(contents).not.toContain('<ORG_NAME>');
61
+ });
62
+ it('trims whitespace around the orgName input', async () => {
63
+ nextAnswer = ' acme ';
64
+ await new create_1.CreateCommand().run();
65
+ expect(fs.existsSync(path.join(tempDir, 'acme-transformMapping.ts'))).toBe(true);
66
+ });
67
+ it('throws when orgName is empty', async () => {
68
+ nextAnswer = ' ';
69
+ await expect(new create_1.CreateCommand().run()).rejects.toThrow(/orgName is required/);
70
+ });
71
+ it('refuses to overwrite an existing file', async () => {
72
+ const existing = path.join(tempDir, 'acme-transformMapping.ts');
73
+ fs.writeFileSync(existing, 'do not clobber', 'utf8');
74
+ nextAnswer = 'acme';
75
+ await expect(new create_1.CreateCommand().run()).rejects.toThrow(/Refusing to overwrite/);
76
+ expect(fs.readFileSync(existing, 'utf8')).toEqual('do not clobber');
77
+ });
78
+ });
@@ -0,0 +1,17 @@
1
+ interface UploadOptions {
2
+ filePath: string;
3
+ message?: string;
4
+ branch?: string;
5
+ skipGit: boolean;
6
+ }
7
+ export declare class UploadCommand {
8
+ static parseArgs(args: string[]): UploadOptions;
9
+ static buildCommitMessage(userMessage: string, fileId: string): string;
10
+ private prompt;
11
+ private promptHidden;
12
+ private runGit;
13
+ private tryRunGit;
14
+ private commitToGit;
15
+ run(args: string[]): Promise<void>;
16
+ }
17
+ export {};
@@ -0,0 +1,228 @@
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.UploadCommand = void 0;
27
+ const child_process_1 = require("child_process");
28
+ const fs = __importStar(require("fs"));
29
+ const path = __importStar(require("path"));
30
+ const readline = __importStar(require("readline"));
31
+ const sdk_1 = require("@contrail/sdk");
32
+ const compile_1 = require("./compile");
33
+ class UploadCommand {
34
+ static parseArgs(args) {
35
+ let filePath;
36
+ let message;
37
+ let branch;
38
+ let skipGit = false;
39
+ for (let i = 0; i < args.length; i++) {
40
+ const a = args[i];
41
+ if (a === '-m') {
42
+ message = args[++i];
43
+ if (message === undefined) {
44
+ throw new Error('-m requires a commit message argument');
45
+ }
46
+ }
47
+ else if (a === '-b') {
48
+ branch = args[++i];
49
+ if (branch === undefined) {
50
+ throw new Error('-b requires a branch name argument');
51
+ }
52
+ }
53
+ else if (a === '--skip-git' || a === '--skipGit') {
54
+ skipGit = true;
55
+ }
56
+ else if (a.startsWith('-')) {
57
+ throw new Error(`Unknown option: ${a}`);
58
+ }
59
+ else if (!filePath) {
60
+ filePath = a;
61
+ }
62
+ else {
63
+ throw new Error(`Unexpected argument: ${a}`);
64
+ }
65
+ }
66
+ if (!filePath) {
67
+ throw new Error('upload: missing <path.ts> argument');
68
+ }
69
+ return { filePath, message, branch, skipGit };
70
+ }
71
+ static buildCommitMessage(userMessage, fileId) {
72
+ const lines = userMessage.split(/\r?\n/);
73
+ lines[0] = `${lines[0]} [fileId: ${fileId}]`;
74
+ return lines.join('\n');
75
+ }
76
+ prompt(question) {
77
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
78
+ return new Promise((resolve) => {
79
+ rl.question(question, (answer) => {
80
+ rl.close();
81
+ resolve(answer.trim());
82
+ });
83
+ });
84
+ }
85
+ promptHidden(question) {
86
+ return new Promise((resolve) => {
87
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: true });
88
+ const rlAny = rl;
89
+ rlAny._writeToOutput = (str) => {
90
+ if (str.includes(question)) {
91
+ rlAny.output.write(str);
92
+ }
93
+ };
94
+ rl.question(question, (answer) => {
95
+ rl.close();
96
+ process.stdout.write('\n');
97
+ resolve(answer);
98
+ });
99
+ });
100
+ }
101
+ runGit(args, cwd) {
102
+ return (0, child_process_1.execFileSync)('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
103
+ }
104
+ tryRunGit(args, cwd) {
105
+ try {
106
+ const stdout = this.runGit(args, cwd);
107
+ return { ok: true, stdout, stderr: '' };
108
+ }
109
+ catch (err) {
110
+ return {
111
+ ok: false,
112
+ stdout: err && err.stdout ? err.stdout.toString() : '',
113
+ stderr: err && err.stderr ? err.stderr.toString() : (err && err.message) || '',
114
+ };
115
+ }
116
+ }
117
+ async commitToGit(absPaths, fileId, options) {
118
+ const repoDir = path.dirname(absPaths[0]);
119
+ const relPaths = absPaths.map((p) => path.basename(p));
120
+ const versionCheck = this.tryRunGit(['--version'], repoDir);
121
+ if (!versionCheck.ok) {
122
+ console.log('git command not available; skipping git commit.');
123
+ return;
124
+ }
125
+ const insideRepo = this.tryRunGit(['rev-parse', '--is-inside-work-tree'], repoDir);
126
+ if (!insideRepo.ok || insideRepo.stdout.trim() !== 'true') {
127
+ console.log(`Not inside a git working tree (${repoDir}); skipping git commit.`);
128
+ return;
129
+ }
130
+ for (const relPath of relPaths) {
131
+ const tracked = this.tryRunGit(['ls-files', '--error-unmatch', relPath], repoDir);
132
+ if (!tracked.ok) {
133
+ const answer = (await this.prompt(`File is not tracked by git: ${relPath}\nAdd it to git? (Y/n): `)).toLowerCase();
134
+ if (answer === 'n' || answer === 'no') {
135
+ console.log('Nothing was done in git.');
136
+ return;
137
+ }
138
+ }
139
+ }
140
+ if (options.branch) {
141
+ const branchResult = this.tryRunGit(['checkout', '-b', options.branch], repoDir);
142
+ if (!branchResult.ok) {
143
+ throw new Error(`Failed to create branch "${options.branch}": ${branchResult.stderr.trim()}`);
144
+ }
145
+ console.log(`Created and switched to branch "${options.branch}"`);
146
+ }
147
+ let message = options.message;
148
+ if (!message) {
149
+ message = await this.prompt('Commit message: ');
150
+ if (!message) {
151
+ throw new Error('A commit message is required');
152
+ }
153
+ }
154
+ const finalMessage = UploadCommand.buildCommitMessage(message, fileId);
155
+ const addResult = this.tryRunGit(['add', '--', ...relPaths], repoDir);
156
+ if (!addResult.ok) {
157
+ throw new Error(`git add failed: ${addResult.stderr.trim()}`);
158
+ }
159
+ const commitResult = this.tryRunGit(['commit', '-m', finalMessage, '--', ...relPaths], repoDir);
160
+ if (!commitResult.ok) {
161
+ throw new Error(`git commit failed: ${commitResult.stderr.trim() || commitResult.stdout.trim()}`);
162
+ }
163
+ console.log(commitResult.stdout.trim());
164
+ }
165
+ async run(args) {
166
+ const options = UploadCommand.parseArgs(args);
167
+ const tsAbsPath = path.resolve(process.cwd(), options.filePath);
168
+ if (!fs.existsSync(tsAbsPath)) {
169
+ throw new Error(`File not found: ${tsAbsPath}`);
170
+ }
171
+ if (!tsAbsPath.endsWith('.ts')) {
172
+ throw new Error(`Expected a .ts file, got: ${tsAbsPath}`);
173
+ }
174
+ await new compile_1.CompileCommand().run(tsAbsPath);
175
+ const absPath = tsAbsPath.replace(/\.ts$/, '.js');
176
+ if (!fs.existsSync(absPath)) {
177
+ throw new Error(`Compiled file not found: ${absPath}`);
178
+ }
179
+ let email = process.env.CONTRAIL_CLI_EMAIL;
180
+ let password = process.env.CONTRAIL_CLI_PASSWORD;
181
+ if (!email) {
182
+ email = await this.prompt('Email: ');
183
+ }
184
+ if (!password) {
185
+ password = await this.promptHidden('Password: ');
186
+ }
187
+ if (!email || !password) {
188
+ throw new Error('Email and password are required');
189
+ }
190
+ delete require.cache[absPath];
191
+ const loaded = require(absPath);
192
+ const mapping = loaded && loaded.mapping;
193
+ if (!mapping || !mapping.orgInfo) {
194
+ throw new Error(`Mapping file is missing "mapping.orgInfo" export: ${absPath}`);
195
+ }
196
+ const { orgName, appIdentifier } = mapping.orgInfo;
197
+ if (!orgName) {
198
+ throw new Error(`Mapping file is missing "mapping.orgInfo.orgName": ${absPath}`);
199
+ }
200
+ if (!appIdentifier) {
201
+ throw new Error(`Mapping file is missing "mapping.orgInfo.appIdentifier": ${absPath}`);
202
+ }
203
+ await (0, sdk_1.login)({ orgSlug: orgName, email, password });
204
+ console.log(`Logged in to org "${orgName}" as ${email}`);
205
+ const apps = await new sdk_1.Entities().get({
206
+ entityName: 'app',
207
+ criteria: { identifier: appIdentifier },
208
+ });
209
+ if (!apps || apps.length !== 1) {
210
+ throw new Error(`Expected exactly one app with identifier "${appIdentifier}" in org "${orgName}", found ${apps ? apps.length : 0}`);
211
+ }
212
+ const app = apps[0];
213
+ const buffer = fs.readFileSync(absPath);
214
+ const fileName = path.basename(absPath);
215
+ const fileOwner = `app:${app.id}`;
216
+ const uploadedFile = await new sdk_1.Files().createAndUploadFileFromBuffer(buffer, 'application/javascript', fileName, fileOwner);
217
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
218
+ const responsePath = `${absPath}.uploaded-${timestamp}.json`;
219
+ fs.writeFileSync(responsePath, JSON.stringify(uploadedFile, null, 2), 'utf8');
220
+ console.log(`Wrote response to ${responsePath}`);
221
+ console.log(`FILE ID: ${uploadedFile.id}`);
222
+ if (options.skipGit) {
223
+ return;
224
+ }
225
+ await this.commitToGit([tsAbsPath, absPath], uploadedFile.id, options);
226
+ }
227
+ }
228
+ exports.UploadCommand = UploadCommand;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ jest.mock('@contrail/sdk', () => ({
4
+ Entities: jest.fn(),
5
+ Files: jest.fn(),
6
+ login: jest.fn(),
7
+ }));
8
+ const upload_1 = require("./upload");
9
+ describe('UploadCommand.parseArgs', () => {
10
+ it('parses a bare file path', () => {
11
+ const opts = upload_1.UploadCommand.parseArgs(['mapping.ts']);
12
+ expect(opts).toEqual({
13
+ filePath: 'mapping.ts',
14
+ message: undefined,
15
+ branch: undefined,
16
+ skipGit: false,
17
+ });
18
+ });
19
+ it('parses -m commit message option', () => {
20
+ const opts = upload_1.UploadCommand.parseArgs(['mapping.ts', '-m', 'my message']);
21
+ expect(opts.message).toEqual('my message');
22
+ expect(opts.skipGit).toBe(false);
23
+ });
24
+ it('parses -b branch option', () => {
25
+ const opts = upload_1.UploadCommand.parseArgs(['mapping.ts', '-b', 'feature/x']);
26
+ expect(opts.branch).toEqual('feature/x');
27
+ });
28
+ it('parses --skip-git flag', () => {
29
+ const opts = upload_1.UploadCommand.parseArgs(['mapping.ts', '--skip-git']);
30
+ expect(opts.skipGit).toBe(true);
31
+ });
32
+ it('accepts the legacy --skipGit alias', () => {
33
+ const opts = upload_1.UploadCommand.parseArgs(['mapping.ts', '--skipGit']);
34
+ expect(opts.skipGit).toBe(true);
35
+ });
36
+ it('parses options before the file path', () => {
37
+ const opts = upload_1.UploadCommand.parseArgs(['-m', 'msg', '-b', 'br', 'mapping.ts']);
38
+ expect(opts).toEqual({
39
+ filePath: 'mapping.ts',
40
+ message: 'msg',
41
+ branch: 'br',
42
+ skipGit: false,
43
+ });
44
+ });
45
+ it('parses all options together', () => {
46
+ const opts = upload_1.UploadCommand.parseArgs(['mapping.ts', '-m', 'msg', '-b', 'br', '--skip-git']);
47
+ expect(opts).toEqual({
48
+ filePath: 'mapping.ts',
49
+ message: 'msg',
50
+ branch: 'br',
51
+ skipGit: true,
52
+ });
53
+ });
54
+ it('throws when -m is missing its value', () => {
55
+ expect(() => upload_1.UploadCommand.parseArgs(['mapping.ts', '-m'])).toThrow(/-m requires a commit message/);
56
+ });
57
+ it('throws when -b is missing its value', () => {
58
+ expect(() => upload_1.UploadCommand.parseArgs(['mapping.ts', '-b'])).toThrow(/-b requires a branch name/);
59
+ });
60
+ it('throws on unknown option', () => {
61
+ expect(() => upload_1.UploadCommand.parseArgs(['mapping.ts', '--bogus'])).toThrow(/Unknown option: --bogus/);
62
+ });
63
+ it('throws when an extra positional argument is supplied', () => {
64
+ expect(() => upload_1.UploadCommand.parseArgs(['mapping.ts', 'extra.ts'])).toThrow(/Unexpected argument: extra\.ts/);
65
+ });
66
+ it('throws when no file path is provided', () => {
67
+ expect(() => upload_1.UploadCommand.parseArgs([])).toThrow(/missing <path\.ts>/);
68
+ });
69
+ it('throws when only options are provided', () => {
70
+ expect(() => upload_1.UploadCommand.parseArgs(['--skip-git'])).toThrow(/missing <path\.ts>/);
71
+ });
72
+ });
73
+ describe('UploadCommand.buildCommitMessage', () => {
74
+ it('appends fileId to the first line of a single-line message', () => {
75
+ expect(upload_1.UploadCommand.buildCommitMessage('initial commit', 'abc123')).toEqual('initial commit [fileId: abc123]');
76
+ });
77
+ it('only appends fileId to the first line of a multi-line message', () => {
78
+ const result = upload_1.UploadCommand.buildCommitMessage('header line\nbody line 1\nbody line 2', 'xyz');
79
+ expect(result).toEqual('header line [fileId: xyz]\nbody line 1\nbody line 2');
80
+ });
81
+ it('handles CRLF line endings', () => {
82
+ const result = upload_1.UploadCommand.buildCommitMessage('header\r\nbody', 'fid');
83
+ expect(result).toEqual('header [fileId: fid]\nbody');
84
+ });
85
+ it('handles an empty message', () => {
86
+ expect(upload_1.UploadCommand.buildCommitMessage('', 'fid')).toEqual(' [fileId: fid]');
87
+ });
88
+ });
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+ export declare class Cli {
3
+ main(): Promise<void>;
4
+ }
5
+ export declare function main(): Promise<void>;