Skip to content

Commit

Permalink
feat: YAML support (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
enxg authored Oct 7, 2021
1 parent 558ed09 commit f69ae95
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 20 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"find-up": "^5.0.0",
"ora": "^5.4.1",
"prompts": "^2.4.1",
"tslib": "^2.3.1"
"tslib": "^2.3.1",
"yaml": "^1.10.2"
},
"devDependencies": {
"@commitlint/cli": "^13.2.0",
Expand Down
14 changes: 9 additions & 5 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { readFile } from 'fs/promises';
import ora from 'ora';
import { join, basename } from 'path';
import { setTimeout } from 'timers/promises';
import YAML from 'yaml';

const { blueBright, red } = chalk;

Expand All @@ -16,7 +17,7 @@ function createComponent(component: string, name: string, config: any, configLoc
if (!projectLanguage) return reject(new Error("There is no 'projectLanguage' field in .sapphirerc.json"));
const template = `${component.toLowerCase()}.${projectLanguage}.sapphire`;

const corePath = `${componentsFolder}components/${template}`;
const corePath = `${componentsFolder.pathname}${template}`;
const userPath = config.customFileTemplates.enabled ? join(configLoc, config.customFileTemplates.location, template) : null;
const target = join(configLoc, config.locations.base, '%L%', `${name}.${projectLanguage}`);
const params = { name: basename(name) };
Expand All @@ -36,7 +37,7 @@ function timeout(ms: number): Promise<null> {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
return setTimeout(ms)
.then(() => {
resolve(null);
return resolve(null);
})
.catch(reject);
});
Expand All @@ -45,7 +46,10 @@ function timeout(ms: number): Promise<null> {
function fetchConfig(): Promise<
Promise<string | undefined> | Promise<null> extends PromiseLike<infer U> ? U : Promise<string | undefined> | Promise<null>
> {
return Promise.race([FindUp('.sapphirerc.json', { cwd: '.' }), timeout(5000)]);
return Promise.race([FindUp('.sapphirerc.json', { cwd: '.' }), timeout(5000)]).then((a) => {
if (a) return a;
return Promise.race([FindUp('.sapphirerc.yml', { cwd: '.' }), timeout(5000)]);
});
}

export default async (component: string, name: string) => {
Expand All @@ -57,10 +61,10 @@ export default async (component: string, name: string) => {

const configLoc = await fetchConfig();
if (!configLoc) return fail("Can't find the Sapphire CLI config.");
const config = JSON.parse(await readFile(configLoc, 'utf8'));
const config = configLoc.endsWith('json') ? JSON.parse(await readFile(configLoc, 'utf8')) : YAML.parse(await readFile(configLoc, 'utf8'));
if (!config) return fail("Can't parse the Sapphire CLI config.");

await createComponent(component, name, config, configLoc.replace('.sapphirerc.json', '')).catch((err) => {
await createComponent(component, name, config, configLoc.replace(/.sapphirerc.(json|yml)/g, '')).catch((err) => {
spinner.fail();
console.log(red(err.message));
process.exit(1);
Expand Down
11 changes: 8 additions & 3 deletions src/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ export default async (name: string, flags: Record<string, boolean>) => {

await rm(`./${response.projectName}/ghr`, { recursive: true, force: true });

await CreateFileFromTemplate('.sapphirerc.json.sapphire', resolve(`./${response.projectName}/.sapphirerc.json`), null, {
language: response.projectLang
});
await CreateFileFromTemplate(
`.sapphirerc.${response.configFormat}.sapphire`,
resolve(`./${response.projectName}/.sapphirerc.${response.configFormat}`),
null,
{
language: response.projectLang
}
);

await editPackageJson(response.projectName, projectName);
};
Expand Down
6 changes: 3 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { URL } from 'url';

export const rootFolder = new URL('../../', import.meta.url);
export const templatesFolder = new URL('templates/', rootFolder);
export const componentsFolder = new URL('components/', templatesFolder);
export const rootFolder = new URL('../', import.meta.url);
export const templatesFolder = new URL('./templates/', rootFolder);
export const componentsFolder = new URL('./components/', templatesFolder);
export const repoUrl = 'https://github.com/sapphiredev/examples.git';
4 changes: 2 additions & 2 deletions src/functions/CreateFileFromTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync } from 'fs';
import { readFile, writeFile, mkdir } from 'fs/promises';
import path from 'path';
import { templatesFolder } from '../constants.js';
import { templatesFolder } from '#constants';

export function CreateFileFromTemplate(
template: string,
Expand All @@ -12,7 +12,7 @@ export function CreateFileFromTemplate(
component = false
) {
return new Promise(async (resolve, reject) => {
const location = custom ? template : `${templatesFolder}${template}`;
const location = custom ? template : `${templatesFolder.pathname}${template}`;

const output = {} as {
f: string;
Expand Down
9 changes: 9 additions & 0 deletions src/prompts/PromptNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export const PromptNew = (projectName: string, yarn: boolean) => {
message: 'Choose a template for your project',
choices: (prev) => (prev === 'ts' ? tsTemplates : jsTemplates)
},
{
type: 'select',
name: 'configFormat',
message: 'What format do you want your config file to be in?',
choices: [
{ title: 'JSON', value: 'json' },
{ title: 'YAML', value: 'yml' }
]
},
{
type: 'select',
name: 'packageManager',
Expand Down
10 changes: 10 additions & 0 deletions templates/.sapphirerc.yml.sapphire
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
projectLanguage: "{{language}}"
locations:
base: src
arguments: arguments
commands: commands
listeners: listeners
preconditions: preconditions
customFileTemplates:
enabled: false
location: ""
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -945,11 +945,6 @@ colorette@^2.0.12:
resolved "https://registry.yarnpkg.com/colorette/-/colorette-2.0.13.tgz#c8d3c7a1b57fbddb0b6b681ee51f8105c4e86eaf"
integrity sha512-lvA4NbohpqUypdfTtJpb5BwhdUrwi1ACLM6uW3lEj0CWKOXrCSJlexv9IgUUN6obat0YGTSy7wfLDLEfOvzFLA==

colors@^1.1.2:
version "1.4.0"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.4.0.tgz#c50491479d4c1bdaed2c9ced32cf7c7dc2360f78"
integrity sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==

commander@^7.2.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-7.2.0.tgz#a36cb57d0b501ce108e4d20559a150a391d97ab7"
Expand Down Expand Up @@ -3825,7 +3820,7 @@ yallist@^4.0.0:
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==

yaml@^1.10.0:
yaml@^1.10.0, yaml@^1.10.2:
version "1.10.2"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==
Expand Down

0 comments on commit f69ae95

Please sign in to comment.