Skip to content

Commit bb4e690

Browse files
Merge pull request #158 from rootstrap/feat/cli-init-git-branches
feat(cli): create develop, qa and main branches on project creation
2 parents c937ca5 + 0d812c7 commit bb4e690

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

‎cli/setup-project.js‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,35 @@ const ProjectFilesManager = require('./project-files-manager.js');
1212
*/
1313
let projectFilesManager;
1414

15+
const DEFAULT_BRANCH = 'develop';
16+
const ADDITIONAL_BRANCHES = ['qa', 'main'];
17+
18+
const hasGitIdentity = async (options) => {
19+
const name = await execShellCommand('git config user.name || true', options);
20+
const email = await execShellCommand('git config user.email || true', options);
21+
return Boolean(name.trim() && email.trim());
22+
};
23+
24+
// Branches can only be created once a commit exists, so the initial commit
25+
// happens before renaming the current branch and branching off it.
1526
const initializeProjectRepository = async (projectName) => {
16-
const escapedProjectName = escapeShellArg(projectName);
17-
await execShellCommand(`cd ${escapedProjectName} && git init && cd ..`);
27+
const options = { cwd: projectName };
28+
29+
await execShellCommand('git init', options);
30+
await execShellCommand('git add -A', options);
31+
32+
const identityFlags = (await hasGitIdentity(options))
33+
? ''
34+
: '-c user.name="Rootstrap Template" -c user.email="template@rootstrap.com"';
35+
await execShellCommand(
36+
`git ${identityFlags} commit -m "chore: initial commit from Rootstrap React Native template"`,
37+
options,
38+
);
39+
await execShellCommand(`git branch -M ${DEFAULT_BRANCH}`, options);
40+
41+
for (const branch of ADDITIONAL_BRANCHES) {
42+
await execShellCommand(`git branch ${branch}`, options);
43+
}
1844
};
1945

2046
const installDependencies = async (projectName) => {
@@ -54,7 +80,7 @@ const removeUnrelatedFiles = () => {
5480
};
5581

5682
// Update package.json infos, name and set version to 0.0.1 + add initial version to rsMetadata
57-
const updatePackageJson = async (projectName) => {
83+
const updatePackageJson = (projectName) => {
5884
const packageJsonPath =
5985
projectFilesManager.getAbsoluteFilePath('package.json');
6086

@@ -75,7 +101,7 @@ const updatePackageJson = async (projectName) => {
75101
fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });
76102
};
77103

78-
const updateProjectConfig = async (projectName) => {
104+
const updateProjectConfig = (projectName) => {
79105
projectFilesManager.replaceFilesContent([
80106
{
81107
fileName: 'env.js',
@@ -227,12 +253,13 @@ const setupProject = async (projectName) => {
227253

228254
try {
229255
removeUnrelatedFiles();
230-
await initializeProjectRepository(projectName);
231256
updatePackageJson(projectName);
232257
updateProjectConfig(projectName);
233258
updateGitHubWorkflows(projectName);
234259
updateProjectReadme(projectName);
235260
updateAgentDocs(projectName);
261+
// Runs last so the initial commit contains the fully set up project
262+
await initializeProjectRepository(projectName);
236263
consola.success(`Clean up and setup your project 🧹`);
237264
} catch (error) {
238265
consola.error(`Failed to clean up project folder`, error);

‎cli/utils.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ const UPSTREAM_REPOSITORY = "obytes/react-native-template-obytes";
66
const TEMPLATE_REPOSITORY = "rootstrap/react-native-template";
77

88
const escapeShellArg = (value) => {
9-
return `'${String(value).replaceAll(/'/g, "'\\''")}'`;
9+
return `'${String(value).replaceAll("'", "'\\''")}'`;
1010
};
1111

12-
const execShellCommand = (cmd) => {
12+
const execShellCommand = (cmd, options) => {
1313
return new Promise((resolve, reject) => {
14-
exec(cmd, (error, stdout, stderr) => {
14+
exec(cmd, options, (error, stdout, stderr) => {
1515
if (error) {
1616
console.warn(error);
1717
reject(error);
18+
} else {
19+
resolve(stdout || stderr);
1820
}
19-
resolve(stdout || stderr);
2021
});
2122
});
2223
};

0 commit comments

Comments
 (0)