create-sparkling-app 2.0.0-rc.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/LICENSE +201 -0
- package/bin/index.js +2 -0
- package/dist/build.js +118 -0
- package/dist/core/actions/action-runner.js +74 -0
- package/dist/core/actions/action.js +2 -0
- package/dist/core/config.js +13 -0
- package/dist/core/project-builder/project-builder.js +205 -0
- package/dist/core/project-builder/template.js +219 -0
- package/dist/create-app/android-dsl.js +69 -0
- package/dist/create-app/constants.js +33 -0
- package/dist/create-app/fs-utils.js +18 -0
- package/dist/create-app/namespace.js +99 -0
- package/dist/create-app/post-create.js +153 -0
- package/dist/create-app/template-manager.js +214 -0
- package/dist/create-app/types.js +2 -0
- package/dist/create-app/user-prompts.js +257 -0
- package/dist/create-app.js +238 -0
- package/dist/help.js +18 -0
- package/dist/index.js +81 -0
- package/dist/init.js +57 -0
- package/dist/logger.js +33 -0
- package/dist/ui.js +79 -0
- package/dist/util.js +22 -0
- package/dist/utils/common.js +39 -0
- package/dist/utils/file-templater.js +120 -0
- package/dist/utils/spinner.js +30 -0
- package/dist/utils/verbose.js +24 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/bin/index.js
ADDED
package/dist/build.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) 2025 TikTok Pte. Ltd.
|
|
3
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
4
|
+
// LICENSE file in the root directory of this source tree.
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
const child_process_1 = require("child_process");
|
|
10
|
+
const util_1 = require("util");
|
|
11
|
+
const execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
12
|
+
const path_1 = __importDefault(require("path"));
|
|
13
|
+
const util_2 = require("./util");
|
|
14
|
+
const runCommand = (command, args, cwd) => {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
console.log(`Executing: ${command} ${args.join(' ')} in ${cwd}`);
|
|
17
|
+
const child = (0, child_process_1.spawn)(command, args, { cwd, stdio: 'inherit' });
|
|
18
|
+
child.on('error', (error) => {
|
|
19
|
+
console.error(`Error with command ${command}: ${error}`);
|
|
20
|
+
reject(error);
|
|
21
|
+
});
|
|
22
|
+
child.on('exit', (code) => {
|
|
23
|
+
if (code !== 0) {
|
|
24
|
+
const errorMsg = `Command ${command} exited with code ${code}`;
|
|
25
|
+
console.error(errorMsg);
|
|
26
|
+
reject(new Error(errorMsg));
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
resolve();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
const buildFrontend = (projectDir) => {
|
|
35
|
+
const command = 'pnpm';
|
|
36
|
+
const args = ['run', 'dev'];
|
|
37
|
+
return runCommand(command, args, projectDir);
|
|
38
|
+
};
|
|
39
|
+
const buildAndroid = (projectDir) => {
|
|
40
|
+
const androidDir = path_1.default.join(projectDir, 'android');
|
|
41
|
+
const command = './gradlew';
|
|
42
|
+
const args = ['build'];
|
|
43
|
+
return runCommand(command, args, androidDir);
|
|
44
|
+
};
|
|
45
|
+
const buildIos = async (projectDir) => {
|
|
46
|
+
const iosDir = path_1.default.join(projectDir, 'ios');
|
|
47
|
+
try {
|
|
48
|
+
const scheme = 'SparklingGo';
|
|
49
|
+
const workspace = 'SparklingGo.xcworkspace';
|
|
50
|
+
const configuration = 'Debug';
|
|
51
|
+
const build_dir = path_1.default.join(iosDir, 'build');
|
|
52
|
+
console.log('Finding available simulator...');
|
|
53
|
+
const { stdout: devicesOutput } = await execAsync('xcrun simctl list devices available');
|
|
54
|
+
const simulatorLine = devicesOutput.split('\n').find(line => line.includes('iPhone')) ?? '';
|
|
55
|
+
if (!simulatorLine) {
|
|
56
|
+
throw new Error('No available iPhone simulator found.');
|
|
57
|
+
}
|
|
58
|
+
const simulatorName = simulatorLine.split('(')[0].trim();
|
|
59
|
+
console.log(`Using simulator: ${simulatorName}`);
|
|
60
|
+
const destination = `platform=iOS Simulator,name=${simulatorName},OS=latest`;
|
|
61
|
+
await runCommand('xcodebuild', ['clean', '-workspace', workspace, '-scheme', scheme, '-configuration', configuration], iosDir);
|
|
62
|
+
await runCommand('xcodebuild', ['build', '-workspace', workspace, '-scheme', scheme, '-configuration', configuration, '-destination', destination, '-derivedDataPath', build_dir], iosDir);
|
|
63
|
+
const { stdout: appPath } = await execAsync(`find "${build_dir}" -name "*.app" | head -n 1`);
|
|
64
|
+
if (!appPath) {
|
|
65
|
+
throw new Error('.app file not found.');
|
|
66
|
+
}
|
|
67
|
+
console.log(`App found at: ${appPath.trim()}`);
|
|
68
|
+
try {
|
|
69
|
+
await execAsync(`xcrun simctl boot "${simulatorName}"`);
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
console.log('Simulator already booted.');
|
|
73
|
+
}
|
|
74
|
+
await runCommand('xcrun', ['simctl', 'install', 'booted', appPath.trim()], projectDir);
|
|
75
|
+
const { stdout: bundleId } = await execAsync(`/usr/libexec/PlistBuddy -c "Print:CFBundleIdentifier" "${appPath.trim()}/Info.plist"`);
|
|
76
|
+
await runCommand('xcrun', ['simctl', 'launch', 'booted', bundleId.trim()], projectDir);
|
|
77
|
+
console.log('iOS build completed successfully.');
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
console.error(`iOS build failed: ${error.message}`);
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const buildAll = async (projectDir) => {
|
|
85
|
+
try {
|
|
86
|
+
await Promise.all([
|
|
87
|
+
buildFrontend(projectDir),
|
|
88
|
+
buildAndroid(projectDir),
|
|
89
|
+
buildIos(projectDir),
|
|
90
|
+
]);
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
console.error('A build step failed. Aborting.');
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
exports.default = async (buildType) => {
|
|
98
|
+
console.log('Building Sparkling Lynx project...');
|
|
99
|
+
const projectRoot = (0, util_2.findSparklingProjectRoot)();
|
|
100
|
+
if (!projectRoot) {
|
|
101
|
+
throw new Error('Failed to find Sparkling project root');
|
|
102
|
+
}
|
|
103
|
+
if (!buildType || buildType === 'all') {
|
|
104
|
+
await buildAll(projectRoot);
|
|
105
|
+
}
|
|
106
|
+
else if (buildType === 'frontend') {
|
|
107
|
+
await buildFrontend(projectRoot);
|
|
108
|
+
}
|
|
109
|
+
else if (buildType === 'android') {
|
|
110
|
+
await buildAndroid(projectRoot);
|
|
111
|
+
}
|
|
112
|
+
else if (buildType === 'ios') {
|
|
113
|
+
await buildIos(projectRoot);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
throw new Error(`Invalid build type: ${buildType}`);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
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.ActionRunner = void 0;
|
|
7
|
+
exports.executeAndLogAction = executeAndLogAction;
|
|
8
|
+
// Copyright (c) 2025 TikTok Pte. Ltd.
|
|
9
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
10
|
+
// LICENSE file in the root directory of this source tree.
|
|
11
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
12
|
+
const logger_1 = require("../../logger");
|
|
13
|
+
const ui_1 = require("../../ui");
|
|
14
|
+
const spinner_1 = require("../../utils/spinner");
|
|
15
|
+
class ActionRunner {
|
|
16
|
+
actions = [];
|
|
17
|
+
context;
|
|
18
|
+
constructor(context) {
|
|
19
|
+
this.context = { ...context, logger: context.logger ?? logger_1.defaultLogger };
|
|
20
|
+
}
|
|
21
|
+
addAction(action) {
|
|
22
|
+
this.actions.push(action);
|
|
23
|
+
}
|
|
24
|
+
async run() {
|
|
25
|
+
let previousResult;
|
|
26
|
+
this.context.logger = logger_1.defaultLogger;
|
|
27
|
+
this.context.logger.info('Running create-app actions...');
|
|
28
|
+
for (const action of this.actions) {
|
|
29
|
+
const spin = (0, spinner_1.createSpinner)();
|
|
30
|
+
this.context.spinner = spin;
|
|
31
|
+
spin.start(`Run action: ${action.name}`);
|
|
32
|
+
const startTime = Date.now();
|
|
33
|
+
try {
|
|
34
|
+
const currentResult = await action.execute(this.context, previousResult);
|
|
35
|
+
const endTime = Date.now();
|
|
36
|
+
spin.stop(`Finished action: ${action.name} (took ${endTime - startTime}ms)`);
|
|
37
|
+
if (currentResult.crucialOutputPaths && currentResult.crucialOutputPaths.length > 0) {
|
|
38
|
+
console.log('');
|
|
39
|
+
console.log(ui_1.ui.info('Output paths:'));
|
|
40
|
+
for (const outputPath of currentResult.crucialOutputPaths) {
|
|
41
|
+
console.log(ui_1.ui.info(` - ${node_path_1.default.resolve(outputPath)}`));
|
|
42
|
+
}
|
|
43
|
+
console.log('');
|
|
44
|
+
}
|
|
45
|
+
previousResult = currentResult;
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
spin.stop(`Action failed: ${action.name}`);
|
|
49
|
+
console.error(ui_1.ui.error(String(error)));
|
|
50
|
+
if (this.context.logger.logFile) {
|
|
51
|
+
console.log(ui_1.ui.info(`See log at ${this.context.logger.logFile}`));
|
|
52
|
+
}
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
console.log(ui_1.ui.success('All actions completed.'));
|
|
57
|
+
this.context.logger.clear();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.ActionRunner = ActionRunner;
|
|
61
|
+
async function executeAndLogAction(action, context, previousResult) {
|
|
62
|
+
context.logger.info(`Starting action: ${action.name}`);
|
|
63
|
+
const startTime = Date.now();
|
|
64
|
+
const result = await action.execute(context, previousResult);
|
|
65
|
+
const endTime = Date.now();
|
|
66
|
+
context.logger.info(`Finished action: ${action.name} (took ${endTime - startTime}ms)`);
|
|
67
|
+
if (result.outputPaths && result.outputPaths.length > 0) {
|
|
68
|
+
context.logger.info('Output paths:');
|
|
69
|
+
for (const outputPath of result.outputPaths) {
|
|
70
|
+
context.logger.info(` - ${outputPath}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
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.saveConfig = saveConfig;
|
|
7
|
+
// Copyright (c) 2025 TikTok Pte. Ltd.
|
|
8
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
9
|
+
// LICENSE file in the root directory of this source tree.
|
|
10
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
11
|
+
function saveConfig(filePath, config) {
|
|
12
|
+
node_fs_1.default.writeFileSync(filePath, `${JSON.stringify(config, null, 2)}\n`, 'utf8');
|
|
13
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
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.ProjectBuilder = void 0;
|
|
7
|
+
// Copyright (c) 2025 TikTok Pte. Ltd.
|
|
8
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
9
|
+
// LICENSE file in the root directory of this source tree.
|
|
10
|
+
const inquirer_1 = __importDefault(require("inquirer"));
|
|
11
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
12
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
13
|
+
const logger_1 = require("../../logger");
|
|
14
|
+
const common_1 = require("../../utils/common");
|
|
15
|
+
const action_runner_1 = require("../actions/action-runner");
|
|
16
|
+
const template_1 = require("./template");
|
|
17
|
+
const template_2 = require("./template");
|
|
18
|
+
class ProjectBuilder {
|
|
19
|
+
config;
|
|
20
|
+
steps = [];
|
|
21
|
+
constructor(config) {
|
|
22
|
+
this.config = config;
|
|
23
|
+
}
|
|
24
|
+
static create(config) {
|
|
25
|
+
return new ProjectBuilder(config);
|
|
26
|
+
}
|
|
27
|
+
addStep(step) {
|
|
28
|
+
if (!step.from && !step.preHook && !step.postHook) {
|
|
29
|
+
throw new Error('Template step must define a source or hook.');
|
|
30
|
+
}
|
|
31
|
+
this.steps.push(step);
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
addSteps(steps) {
|
|
35
|
+
this.steps.push(...steps);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
async build() {
|
|
39
|
+
if (this.steps.length === 0) {
|
|
40
|
+
throw new Error('No template steps configured.');
|
|
41
|
+
}
|
|
42
|
+
const shouldCheckEmpty = this.config.checkEmpty ?? true;
|
|
43
|
+
if (shouldCheckEmpty && !this.config.override && node_fs_1.default.existsSync(this.config.targetDir) && !(0, common_1.isEmptyDir)(this.config.targetDir)) {
|
|
44
|
+
let option;
|
|
45
|
+
try {
|
|
46
|
+
const answer = await inquirer_1.default.prompt([
|
|
47
|
+
{
|
|
48
|
+
type: 'list',
|
|
49
|
+
name: 'choice',
|
|
50
|
+
message: `"${node_path_1.default.basename(this.config.targetDir)}" is not empty, please choose:`,
|
|
51
|
+
choices: [
|
|
52
|
+
{ name: 'Continue and override files', value: 'yes' },
|
|
53
|
+
{ name: 'Cancel operation', value: 'no' },
|
|
54
|
+
],
|
|
55
|
+
},
|
|
56
|
+
]);
|
|
57
|
+
option = (0, template_1.checkCancel)(answer.choice);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
throw new template_1.UserCancelledError(error.message);
|
|
61
|
+
}
|
|
62
|
+
if (option === 'no') {
|
|
63
|
+
throw new template_1.UserCancelledError();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (let i = 0; i < this.steps.length; i++) {
|
|
67
|
+
await this.executeStepWithHooks(this.steps[i], i === 0);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async buildWithActionRunner(context) {
|
|
71
|
+
const runner = new action_runner_1.ActionRunner(context);
|
|
72
|
+
runner.addAction(this.toSingleAction());
|
|
73
|
+
await runner.run();
|
|
74
|
+
}
|
|
75
|
+
toActions() {
|
|
76
|
+
const actions = [];
|
|
77
|
+
const shouldCheckEmpty = this.config.checkEmpty ?? true;
|
|
78
|
+
if (shouldCheckEmpty) {
|
|
79
|
+
actions.push(this.createDirectoryCheckAction());
|
|
80
|
+
}
|
|
81
|
+
for (const [index, step] of this.steps.entries()) {
|
|
82
|
+
actions.push(this.createStepAction(step, index));
|
|
83
|
+
}
|
|
84
|
+
return actions;
|
|
85
|
+
}
|
|
86
|
+
toSingleAction(name = 'project-builder', description) {
|
|
87
|
+
if (this.steps.length === 0) {
|
|
88
|
+
throw new Error('No template steps configured.');
|
|
89
|
+
}
|
|
90
|
+
const { config } = this;
|
|
91
|
+
const steps = [...this.steps];
|
|
92
|
+
const executeStepWithHooks = this.executeStepWithHooks.bind(this);
|
|
93
|
+
logger_1.defaultLogger.info(`Building project with ${steps.length} steps`);
|
|
94
|
+
return {
|
|
95
|
+
description: description ?? `Build project with ${steps.length} steps`,
|
|
96
|
+
name,
|
|
97
|
+
async execute(_context) {
|
|
98
|
+
const shouldCheckEmpty = config.checkEmpty ?? true;
|
|
99
|
+
if (shouldCheckEmpty && !(0, common_1.isEmptyDir)(config.targetDir)) {
|
|
100
|
+
let shouldContinue = false;
|
|
101
|
+
try {
|
|
102
|
+
const answer = await inquirer_1.default.prompt([
|
|
103
|
+
{
|
|
104
|
+
type: 'confirm',
|
|
105
|
+
name: 'cont',
|
|
106
|
+
message: `Target directory ${config.targetDir} is not empty. Continue?`,
|
|
107
|
+
default: false,
|
|
108
|
+
},
|
|
109
|
+
]);
|
|
110
|
+
shouldContinue = (0, template_1.checkCancel)(answer.cont);
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
throw new template_1.UserCancelledError(error.message);
|
|
114
|
+
}
|
|
115
|
+
if (!shouldContinue) {
|
|
116
|
+
throw new template_1.UserCancelledError();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
for (let i = 0; i < steps.length; i++) {
|
|
120
|
+
await executeStepWithHooks(steps[i], i === 0);
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
result: undefined,
|
|
124
|
+
crucialOutputPaths: [config.targetDir],
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
createDirectoryCheckAction() {
|
|
130
|
+
const { targetDir } = this.config;
|
|
131
|
+
return {
|
|
132
|
+
name: 'check-target-directory',
|
|
133
|
+
description: 'Ensure target directory is empty before scaffolding',
|
|
134
|
+
async execute() {
|
|
135
|
+
if (node_fs_1.default.existsSync(targetDir) && !(0, common_1.isEmptyDir)(targetDir)) {
|
|
136
|
+
let shouldContinue = false;
|
|
137
|
+
try {
|
|
138
|
+
const answer = await inquirer_1.default.prompt([
|
|
139
|
+
{
|
|
140
|
+
type: 'confirm',
|
|
141
|
+
name: 'cont',
|
|
142
|
+
message: `Target directory ${targetDir} is not empty. Continue?`,
|
|
143
|
+
default: false,
|
|
144
|
+
},
|
|
145
|
+
]);
|
|
146
|
+
shouldContinue = (0, template_1.checkCancel)(answer.cont);
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
throw new template_1.UserCancelledError(error.message);
|
|
150
|
+
}
|
|
151
|
+
if (!shouldContinue) {
|
|
152
|
+
throw new template_1.UserCancelledError();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return { result: undefined };
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
createStepAction(step, index) {
|
|
160
|
+
return {
|
|
161
|
+
name: `template-step-${index + 1}`,
|
|
162
|
+
description: step.from ? `Copy template from ${step.from}` : 'Execute template hook',
|
|
163
|
+
execute: async (context) => {
|
|
164
|
+
await this.executeStepWithHooks(step, index === 0, context);
|
|
165
|
+
return { result: undefined };
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
async executeStepWithHooks(step, isFirstStep, context) {
|
|
170
|
+
const preHookSteps = step.preHook ? await step.preHook(this.config, step) : undefined;
|
|
171
|
+
if (preHookSteps) {
|
|
172
|
+
for (const hookStep of Array.isArray(preHookSteps) ? preHookSteps : [preHookSteps]) {
|
|
173
|
+
await this.executeStepWithHooks(hookStep, isFirstStep, context);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (step.from) {
|
|
177
|
+
await this.executeStep(step, isFirstStep);
|
|
178
|
+
}
|
|
179
|
+
const postHookSteps = step.postHook ? await step.postHook(this.config, step) : undefined;
|
|
180
|
+
if (postHookSteps) {
|
|
181
|
+
for (const hookStep of Array.isArray(postHookSteps) ? postHookSteps : [postHookSteps]) {
|
|
182
|
+
await this.executeStepWithHooks(hookStep, false, context);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async executeStep(step, isFirstStep) {
|
|
187
|
+
if (!step.from) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const targetDir = step.to ? node_path_1.default.resolve(this.config.targetDir, step.to) : this.config.targetDir;
|
|
191
|
+
await (0, template_2.copyTemplateWithVariables)({
|
|
192
|
+
checkEmpty: isFirstStep ? this.config.checkEmpty : step.checkEmpty,
|
|
193
|
+
from: step.from,
|
|
194
|
+
isMergePackageJson: step.isMergePackageJson,
|
|
195
|
+
override: step.override ?? this.config.override,
|
|
196
|
+
packageName: step.packageName ?? this.config.packageName,
|
|
197
|
+
renameFiles: step.renameFiles,
|
|
198
|
+
skipFiles: step.skipFiles,
|
|
199
|
+
to: targetDir,
|
|
200
|
+
variables: step.variables,
|
|
201
|
+
version: step.version ?? this.config.version,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
exports.ProjectBuilder = ProjectBuilder;
|