@sapphire/cli 1.5.1-next.6fe3d0c.0 → 1.6.0-next.afa4afb.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,15 +25,26 @@ async function createComponent(component, name, config, configLoc) {
25
25
  else if (await fileExists(corePath)) {
26
26
  return CreateFileFromTemplate(`components/${template}`, target, config, params, false, true);
27
27
  }
28
- throw new Error("Can't find the template.");
28
+ throw new Error(`Couldn't find a template file for that component type.${parseCommonHints(component)}`);
29
29
  }
30
30
  async function fetchConfig() {
31
- const a = await Promise.race([findUp('.sapphirerc.json', { cwd: '.' }), sleep(5000)]);
32
- if (a) {
33
- return a;
31
+ const configFileAsJson = await Promise.race([findUp('.sapphirerc.json', { cwd: '.' }), sleep(5000)]);
32
+ if (configFileAsJson) {
33
+ return configFileAsJson;
34
34
  }
35
35
  return Promise.race([findUp('.sapphirerc.yml', { cwd: '.' }), sleep(5000)]);
36
36
  }
37
+ /**
38
+ * Parses common hints for the user
39
+ * @param component Component name
40
+ * @returns A string with a hint for the user
41
+ */
42
+ function parseCommonHints(component) {
43
+ if (component.toLowerCase() === 'command' || component.toLowerCase() === 'commands') {
44
+ return `\nHint: You wrote "${component}", instead of "messagecommand", "slashcommand", or "contextmenucommand"`;
45
+ }
46
+ return '';
47
+ }
37
48
  export default async (component, name) => {
38
49
  const spinner = new Spinner(`Creating a ${component.toLowerCase()}`).start();
39
50
  const fail = (error, additionalExecution) => {
@@ -45,11 +45,15 @@ async function configureYarnRc(location, name, value) {
45
45
  return true;
46
46
  }
47
47
  async function installYarnV3(location, verbose) {
48
- const value = await execa('yarn', ['set', 'version', 'berry'], {
48
+ const valueSetVersion = await execa('yarn', ['set', 'version', 'berry'], {
49
49
  stdio: verbose ? 'inherit' : undefined,
50
50
  cwd: `./${location}/`
51
51
  });
52
- if (value.exitCode !== 0) {
52
+ const valueInstallPlugins = await execa('yarn', ['plugin', 'import', 'interactive-tools'], {
53
+ stdio: verbose ? 'inherit' : undefined,
54
+ cwd: `./${location}/`
55
+ });
56
+ if (valueSetVersion.exitCode !== 0 || valueInstallPlugins.exitCode !== 0) {
53
57
  throw new Error('An unknown error occurred while installing Yarn v3. Try running Sapphire CLI with "--verbose" flag.');
54
58
  }
55
59
  await Promise.all([
@@ -6,35 +6,45 @@ export async function CreateFileFromTemplate(template, target, config, params, c
6
6
  const location = custom ? template : `${templatesFolder}${template}`;
7
7
  const output = {};
8
8
  if (component) {
9
- const [c, f] = await getComponentTemplateWithConfig(location);
10
- output.c = c;
11
- output.f = f;
9
+ const [config, templateContent] = await getComponentTemplateWithConfig(location);
10
+ output.config = config;
11
+ output.templateContent = templateContent;
12
12
  }
13
- output.f ??= await readFile(location, 'utf8');
14
- if (!output.f) {
15
- throw new Error("Can't read file.");
13
+ output.templateContent ??= await readFile(location, 'utf8');
14
+ if (!output.templateContent) {
15
+ throw new Error("Couldn't read the template file. Are you sure it exists, the name is correct, and the content is valid?");
16
16
  }
17
17
  if (params) {
18
18
  for (const param of Object.entries(params)) {
19
- output.f = output.f.replaceAll(`{{${param[0]}}}`, param[1]);
19
+ output.templateContent = output.templateContent.replaceAll(`{{${param[0]}}}`, param[1]);
20
20
  }
21
21
  }
22
- if (!output || (component && (!output.c || !output.c.category))) {
23
- throw new Error('Invalid template.');
22
+ if (!output || (component && (!output.config || !output.config.category))) {
23
+ throw new Error('The template is invalid. Please create a valid template structure.');
24
24
  }
25
- const dir = component ? config.locations[output.c.category] : null;
26
- const ta = component ? target.replace('%L%', dir) : target;
27
- if (await fileExists(ta)) {
28
- throw new Error('Component already exists');
25
+ const directoryForOutput = component ? config?.locations[output.config.category] : null;
26
+ const targetPath = component ? target.replace('%L%', directoryForOutput) : target;
27
+ if (await fileExists(targetPath)) {
28
+ throw new Error('A component with the provided name already exists. Please provide a unique name.');
29
29
  }
30
- await writeFileRecursive(ta, output.f);
30
+ await writeFileRecursive(targetPath, output.templateContent);
31
31
  return true;
32
32
  }
33
+ /**
34
+ * Gets the template and the config from a component template
35
+ * @param path Path to the template
36
+ * @returns [config, template] The config and the template
37
+ */
33
38
  async function getComponentTemplateWithConfig(path) {
34
39
  const file = await readFile(path, 'utf8');
35
40
  const fa = file.split(/---(\r\n|\r|\n|)/gm);
36
41
  return [JSON.parse(fa[0]), fa[2]];
37
42
  }
43
+ /**
44
+ * Writes a file recursively
45
+ * @param target Target path
46
+ * @param data Data to write
47
+ */
38
48
  async function writeFileRecursive(target, data) {
39
49
  const resolvedTarget = resolve(target);
40
50
  const dir = dirname(resolvedTarget);
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -63,7 +63,8 @@ export const PromptNew = (projectName, yarn, pnpm) => {
63
63
  {
64
64
  type: (prev) => (prev === 'Yarn' ? 'confirm' : false),
65
65
  name: 'yarnV3',
66
- message: 'Do you want to use Yarn v3?'
66
+ message: 'Do you want to use Yarn v3?',
67
+ initial: true
67
68
  },
68
69
  {
69
70
  type: 'confirm',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire/cli",
3
- "version": "1.5.1-next.6fe3d0c.0",
3
+ "version": "1.6.0-next.afa4afb.0",
4
4
  "description": "CLI for Sapphire Framework",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",