react-native-integrate
Version:
Automate integration of additional code into React Native projects
81 lines (80 loc) • 3.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNewProject = createNewProject;
const child_process_1 = require("child_process");
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const picocolors_1 = __importDefault(require("picocolors"));
const constants_1 = require("../../constants");
const options_1 = require("../../options");
const prompter_1 = require("../../prompter");
const variables_1 = require("../../variables");
const getIosProjectPath_1 = require("../getIosProjectPath");
const getProjectPath_1 = require("../getProjectPath");
async function createNewProject() {
// prepare workspace in tmp folder
const tmpDir = os_1.default.tmpdir();
const workspaceDir = path_1.default.join(tmpDir, constants_1.Constants.TMP_WORKSPACE_DIR);
fs_1.default.mkdirSync(workspaceDir, { recursive: true });
// confirm command to create project
let createCommand = `npx @react-native-community/cli@latest init ${(0, getIosProjectPath_1.getIosProjectName)()}`;
if (options_1.options.get().interactive) {
createCommand = await (0, prompter_1.text)('Enter command to create the new project', {
initialValue: createCommand,
defaultValue: createCommand,
placeholder: createCommand,
});
}
// parse new project name from command
const projectName = createCommand.match(/ init (\w+)/)?.[1];
if (!projectName)
throw new Error('could not find project name from command');
const tmpProjectDir = path_1.default.join(workspaceDir, projectName);
// make sure the tmp project folder does not exist
if (fs_1.default.existsSync(tmpProjectDir)) {
await new Promise(r => fs_1.default.rm(tmpProjectDir, {
recursive: true,
force: true,
}, r));
}
(0, prompter_1.logMessage)(`creating new project at ${picocolors_1.default.yellow(tmpProjectDir)}`);
(0, prompter_1.startSpinner)('running cli');
// execute command
let isDone = false;
const exitCode = await new Promise(resolve => {
const parts = createCommand.split(' ');
const command = parts[0];
const child = (0, child_process_1.spawn)(command, parts.slice(1), {
shell: process.platform == 'win32',
cwd: workspaceDir,
});
child.stdout.on('data', (chunk) => {
if (isDone)
return;
(0, prompter_1.updateSpinner)(`running cli ${picocolors_1.default.gray((0, prompter_1.getLastLine)(chunk.toString('utf8')))}`);
});
child.stderr.on('data', (chunk) => {
if (isDone)
return;
(0, prompter_1.updateSpinner)(`running cli ${picocolors_1.default.gray((0, prompter_1.getLastLine)(chunk.toString('utf8')))}`);
});
child.stdin.write('n'); // do not install cocoapods
child.stdin.end();
child.on('close', code => {
resolve(code ?? 0);
});
});
isDone = true;
if (exitCode !== 0) {
(0, prompter_1.stopSpinner)(`${picocolors_1.default.red('could not create project, command exit with ')}${picocolors_1.default.yellow(exitCode?.toString())}`);
return false;
}
(0, prompter_1.stopSpinner)('run create command');
variables_1.variables.setPredefined('__OLD_PROJECT_DIR__', (0, getProjectPath_1.getProjectPath)());
process.chdir(tmpProjectDir);
return true;
}