Skip to content

Commit e3ce4e2

Browse files
committed
create script: repo has default, don't overwrite dev password
1 parent bdb62c3 commit e3ce4e2

5 files changed

Lines changed: 37 additions & 21 deletions

File tree

commands.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ export default {
4646
{
4747
flags: '-d, --domain <domain>',
4848
description: 'Domain',
49+
type: 'domain',
4950
required: true,
5051
downcase: true,
5152
prompt: true,
5253
},
5354
{
5455
flags: '-r, --repository [repository]',
5556
description: 'Git repository',
57+
type: 'repository',
5658
prompt: true,
5759
},
5860
{
@@ -342,7 +344,6 @@ export default {
342344
flags: '-u, --user-id [string...]',
343345
description: 'Limit to users by ID (can be multiple).',
344346
},
345-
346347
{
347348
flags: '-m, --email [string...]',
348349
description: 'Limit to users by email (can be multiple).',

src/create/index.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,32 @@ import { queueTask, runTasks } from '../utils/tasks.js';
88
import { removeFiles } from '../utils/file.js';
99
import { replaceAll } from '../utils/replace.js';
1010
import { exec, withDir } from '../utils/shell.js';
11+
import { exit } from '../utils/flow.js';
1112
import { getEnvironments, updateServiceYamlEnv } from '../cloud/utils.js';
1213

1314
const BEDROCK_REPO = 'bedrockio/bedrock-core';
1415

1516
export default async function create(options) {
16-
const { project } = options;
17+
let {
18+
project,
19+
domain,
20+
repository,
21+
address,
22+
staging: stagingId,
23+
production: productionId,
24+
password: adminPassword,
25+
} = options;
1726

27+
if (!domain) {
28+
exit('Domain required.');
29+
}
1830
// "project" will accept any casing
1931
const kebab = kebabCase(project);
2032
const under = snakeCase(project);
2133

22-
const {
23-
domain = '',
24-
repository = '',
25-
address = '',
26-
stagingProjectId = `${kebab}-staging`,
27-
productionProjectId = `${kebab}-production`,
28-
'admin-password': adminPassword = '',
29-
} = options;
34+
repository ||= kebab;
35+
stagingId ||= `${kebab}-staging`;
36+
productionId ||= `${kebab}-production`;
3037

3138
queueTask('Clone Repository', async () => {
3239
await cloneRepository(kebab, BEDROCK_REPO);
@@ -35,15 +42,19 @@ export default async function create(options) {
3542

3643
queueTask('Configure', async () => {
3744
const appName = startCase(project);
45+
3846
const JWT_SECRET = await exec('openssl rand -base64 30');
3947
const ADMIN_PASSWORD = adminPassword || randomBytes(8).toString('hex');
4048

41-
await replaceAll('*.{js,md,yml,tf,conf,json,env}', (str) => {
49+
await replaceAll('**/*.{js,md,yml,tf,conf,json,env}', (str, basename) => {
50+
if (basename !== '.env') {
51+
str = str.replace(/ADMIN_PASSWORD=(.+)/g, `ADMIN_PASSWORD=${ADMIN_PASSWORD}`);
52+
}
53+
4254
str = str.replace(/APP_COMPANY_ADDRESS=(.+)/g, `APP_COMPANY_ADDRESS=${address}`);
4355
str = str.replace(/JWT_SECRET=(.+)/g, `JWT_SECRET=${JWT_SECRET}`);
44-
str = str.replace(/ADMIN_PASSWORD=(.+)/g, `ADMIN_PASSWORD=${ADMIN_PASSWORD}`);
45-
str = str.replace(/bedrock-foundation-production/g, productionProjectId);
46-
str = str.replace(/bedrock-foundation-staging/g, stagingProjectId);
56+
str = str.replace(/bedrock-foundation-production/g, productionId);
57+
str = str.replace(/bedrock-foundation-staging/g, stagingId);
4758
str = str.replace(/bedrockio\/bedrock-core/g, repository);
4859
str = str.replace(/bedrock-foundation/g, kebab);
4960
str = str.replace(/bedrock\.foundation/g, domain);

src/utils/file.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ function getRelativePath(dir) {
6060
return path.relative(process.cwd(), dir);
6161
}
6262

63-
export async function readFile(file) {
64-
if (file.match(/\.json$/)) {
63+
export async function readFile(file, coerce = true) {
64+
if (coerce && file.match(/\.json$/)) {
6565
return await loadJson(file);
6666
} else {
6767
return await fs.readFile(file, 'utf8');

src/utils/git.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ export async function cloneRepository(dir, repo) {
1919

2020
export async function initializeRepository(dir, repo) {
2121
const commands = ['git tag bedrock-cut', 'git add .', 'git commit -m "Bedrock Create"'];
22-
if (repo) {
23-
commands.push(`git remote remove origin`);
24-
commands.push(`git remote add origin git@github.com:${repo}.git`);
22+
if (!repo) {
23+
throw new Error('Repository required.');
2524
}
25+
commands.push(`git remote remove origin`);
26+
commands.push(`git remote add origin git@github.com:${repo}.git`);
2627
await exec(commands);
2728
}
2829

src/utils/replace.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { basename } from 'path';
2+
13
import { glob } from 'glob';
24

35
import { readFile, writeFile } from './file.js';
@@ -10,8 +12,9 @@ export async function replaceAll(pattern, fn) {
1012
});
1113
return Promise.all(
1214
files.map(async (filename) => {
13-
const pre = await readFile(filename);
14-
const post = fn(pre);
15+
const pre = await readFile(filename, false);
16+
const base = basename(filename);
17+
const post = fn(pre, base);
1518
if (pre !== post) {
1619
await writeFile(filename, post);
1720
}

0 commit comments

Comments
 (0)