UNPKG

@soleil-se/create-build-config

Version:

Create config for Soleil build scripts.

118 lines (105 loc) 3.5 kB
import prompts from 'prompts'; import fse from 'fs-extra'; import flat from 'flat'; import { dirname } from 'path'; import { fileURLToPath } from 'url'; const { unflatten } = flat; const dir = dirname(fileURLToPath(import.meta.url)); function createProjectConfigQuestions(path) { if (fse.existsSync(`${path}/project_config.json`)) { return []; } return [{ type: 'text', name: 'key', message: 'Environment name?', initial: 'development', validate: (value) => (!value ? 'Environment name is required.' : true), }, { type: 'text', name: 'name', message: 'Site name (house)?', validate: (value) => (!value ? 'Site name is required.' : true), }, { type: 'text', name: 'host', message: 'Site address?', format: (value) => (!value.startsWith('http') ? `https://${value}` : value), validate: (value) => (!value ? 'Site address is required.' : true), }, ]; } function createUserConfigQuestions(path) { const { env } = fse.readJSONSync(`${path}/project_config.json`); const environments = Object .entries(env) .filter(([key]) => key !== 'default') .map(([key, { name, host }], index, arr) => [{ type: 'text', name: `env.${key}.username`, message: `Username for ${name}? (${host})`, initial: (prev, values) => values[`env.${arr[0][0]}.username`], format: (value) => (!value ? '' : value), }, { type: 'password', name: `env.${key}.password`, message: `Password for ${name}? (${host})`, initial: (prev, values) => values[`env.${arr[0][0]}.password`], format: (value) => (!value ? '' : value), }]) .flat(); return [ ...environments, { type: 'text', name: 'webappSign.username', message: 'Username app signing? (https://developer.sitevison.se)', format: (value) => (!value ? '' : value), }, { type: 'password', name: 'webappSign.password', message: 'Password app signing? (https://developer.sitevison.se)', format: (value) => (!value ? '' : value), }]; } function onCancel() { process.exit(0); } async function createProjectConfigFile(path) { const questions = createProjectConfigQuestions(path); if (questions.length) { const { key, name, host } = await prompts(questions, { onCancel }); const config = await fse.readJSON(`${dir}/template/project_config.json`); config.env = { default: key, [key]: { name, host, production: false, }, }; return fse.writeJSON(`${path}/project_config.json`, config, { spaces: 2 }); } return Promise.resolve(); } async function createUserConfigFile(path) { const response = await prompts(createUserConfigQuestions(path), { onCancel }); const { env, webappSign } = unflatten(response); const config = await fse.readJSON(`${dir}/template/user_config.json`); Object.entries(env).forEach(([key, { username, password }]) => { if (username && password) { config.env[key] = { auth: Buffer.from(`${username}:${password}`, 'utf-8').toString('base64') }; } }); if (webappSign.username && webappSign.password) { config.webappSign.auth = Buffer.from(`${webappSign.username}:${webappSign.password}`, 'utf-8').toString('base64'); } return fse.writeJSON(`${path}/user_config.json`, config, { spaces: 2 }); } export default async function main(path = process.cwd()) { await createProjectConfigFile(path); await createUserConfigFile(path); }