Skip to content

Commit 4acdf7f

Browse files
tylerjusflyIfycode
authored andcommitted
fix: changed main.ts, added necessary types, ensured things are not formatted too
1 parent b810120 commit 4acdf7f

File tree

3 files changed

+78
-105
lines changed

3 files changed

+78
-105
lines changed

src/interfaces.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
interface TemplateQuestions {
1+
export interface TemplateQuestions {
22
type: string;
33
name: string;
44
message: string;
55
choices: string[];
66
default: string;
7-
}
7+
}
8+
9+
export type ItemplateOptions = "ts" | "esm" | "cjs";
10+
11+
export interface Ioptions {
12+
skipPrompts: boolean;
13+
git: true;
14+
skipGit: string | undefined;
15+
folderName: string;
16+
template: ItemplateOptions;
17+
targetDirectory: string;
18+
templateDirectory: string;
19+
runInstall: true;
20+
skipInstall: boolean;
21+
help: string | boolean;
22+
version: string | boolean;
23+
}

src/main.ts

Lines changed: 59 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,85 @@
1-
import chalk from "chalk";
2-
import fs from "fs";
3-
import ncp from "ncp";
4-
import path from "path";
5-
import { promisify } from "util";
6-
import execa from "execa";
7-
import Listr from "listr";
8-
import { spawn } from "child_process";
9-
import { userSupport } from "./help";
1+
import chalk from 'chalk';
2+
import fs from 'fs';
3+
import ncp from 'ncp';
4+
import path from 'path';
5+
import { promisify } from 'util';
6+
import execa from 'execa';
7+
import Listr from 'listr';
8+
import { spawn } from 'child_process';
9+
import { userSupport } from './help';
10+
import { Ioptions, ItemplateOptions } from './interfaces';
1011

1112
const access = promisify(fs.access);
1213
const copy = promisify(ncp);
1314

14-
type ItemplateOptions = "ts" | "esm" | "cjs";
15-
16-
let createGitIgnoreFile = async (options: { targetDirectory: string }) => {
17-
const content =
18-
"# See http://help.github.com/ignore-files/ for more about ignoring files.\n\n#.env file\n.env\n\n# dependencies\n/node_modules";
19-
fs.writeFileSync(path.join(options.targetDirectory, ".gitignore"), content);
15+
let createGitIgnoreFile = async (options: {targetDirectory: string}) => {
16+
const content = '# See http://help.github.com/ignore-files/ for more about ignoring files.\n\n#.env file\n.env\n\n# dependencies\n/node_modules';
17+
fs.writeFileSync(path.join(options.targetDirectory, '.gitignore'), content);
2018
return;
21-
};
19+
}
2220

23-
let createEnvFiles = async (options: {
24-
template: ItemplateOptions;
25-
targetDirectory: string;
26-
}) => {
21+
let createEnvFiles = async (options: {template: ItemplateOptions; targetDirectory: string}) => {
2722
let atlasPort;
2823
let localPort;
2924
const assignPort = () => {
30-
if (options.template === "ts") {
25+
if (options.template === 'ts') {
3126
atlasPort = 8070;
3227
localPort = 8071;
3328
}
34-
if (options.template === "esm") {
29+
if (options.template === 'esm') {
3530
atlasPort = 8080;
3631
localPort = 8081;
3732
}
38-
if (options.template === "cjs") {
33+
if (options.template === 'cjs') {
3934
atlasPort = 8090;
4035
localPort = 8091;
4136
}
42-
};
37+
}
4338
assignPort();
4439
const content = `\nPORT_ATLAS=${atlasPort}\nMONGODB_ATLAS_URI=\n\nPORT_LOCAL=${localPort}\nMONGODB_LOCAL_URI=\n\nCLIENT_APP_PORT=\nCLIENT_APP_URL=\n`;
45-
fs.writeFileSync(path.join(options.targetDirectory, ".env.example"), content);
40+
fs.writeFileSync(path.join(options.targetDirectory, '.env.example'), content);
4641
return;
47-
};
42+
}
4843

49-
let createLisenceFiles = async (options: { targetDirectory: string }) => {
50-
const content =
51-
'ISC License (ISC)\n\nCopyright 2022-2023 Author Name\n\nPermission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.\n\nTHE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.';
52-
fs.writeFileSync(path.join(options.targetDirectory, "LICENSE"), content);
44+
let createLisenceFiles = async (options: {targetDirectory: string}) => {
45+
const content = 'ISC License (ISC)\n\nCopyright 2022-2023 Author Name\n\nPermission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.\n\nTHE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.';
46+
fs.writeFileSync(path.join(options.targetDirectory, 'LICENSE'), content);
5347
return;
54-
};
48+
}
5549

56-
let copyTemplateFolderContent = async (options: {
57-
templateDirectory: string;
58-
targetDirectory: string;
59-
}) => {
50+
let copyTemplateFolderContent = async (options: {templateDirectory: string; targetDirectory: string }) => {
6051
return copy(options.templateDirectory, options.targetDirectory, {
61-
clobber: false,
52+
clobber: false
6253
});
63-
};
54+
}
6455

65-
let gitInit = async (options: { git: any; targetDirectory: any }) => {
66-
if (options.git) {
67-
//git init only if git returns true
68-
const result = await execa("git", ["init"], {
69-
cwd: options.targetDirectory,
56+
let gitInit = async (options: {git: boolean | string; targetDirectory: string}) => {
57+
if (options.git) { //git init only if git returns true
58+
const result = await execa('git', ['init'], {
59+
cwd: options.targetDirectory
7060
});
7161

7262
if (result.failed) {
73-
return Promise.reject(new Error("Failed to initialize git"));
63+
return Promise.reject(new Error('Failed to initialize git'));
7464
}
7565
}
7666

7767
return;
78-
};
79-
80-
let npmInstall = async (options: { runInstall: any; targetDirectory: any }) => {
81-
if (options.runInstall) {
82-
//install only if runInstall returns true
83-
if (process.platform === "win32")
84-
spawn("npm.cmd", ["install"], {
85-
cwd: options.targetDirectory,
86-
stdio: "inherit",
87-
});
88-
else
89-
spawn("npm", ["install"], {
90-
cwd: options.targetDirectory,
91-
stdio: "inherit",
92-
});
68+
}
69+
70+
let npmInstall = async (options: {runInstall: boolean; targetDirectory: string}) => {
71+
if (options.runInstall) { //install only if runInstall returns true
72+
if (process.platform === 'win32') spawn('npm.cmd', ['install'], { cwd: options.targetDirectory, stdio: 'inherit' });
73+
else spawn('npm', ['install'], {cwd: options.targetDirectory, stdio: 'inherit'});
9374
}
9475

9576
return;
96-
};
97-
98-
export let downloadTemplateKit = async (options: {
99-
targetDirectory: string;
100-
folderName: string;
101-
template: ItemplateOptions;
102-
templateDirectory: string;
103-
git: any;
104-
runInstall: any;
105-
}) => {
77+
}
78+
79+
export let downloadTemplateKit = async (options: Ioptions) => {
10680
options = {
10781
...options,
108-
targetDirectory: options.targetDirectory || process.cwd(), //root/parent folder at this point
82+
targetDirectory: options.targetDirectory || process.cwd() //root/parent folder at this point
10983
};
11084

11185
//create folder with user input here...
@@ -125,28 +99,20 @@ export let downloadTemplateKit = async (options: {
12599
new URL(currentFileUrl).pathname.indexOf("/") + 1
126100
);
127101

128-
const templateDir = path.resolve(
129-
newUrl,
130-
"../../../templates",
131-
options.template.toLowerCase()
132-
);
102+
const templateDir = path.resolve(newUrl, '../../../templates', options.template.toLowerCase());
133103

134104
options.templateDirectory = templateDir;
135105

136106
try {
137-
await access(templateDir, fs.constants.R_OK).then(async () => {
107+
await access(templateDir, fs.constants.R_OK).then(_ => {
138108
/* rename name option in package.json same as project/folder name */
139-
const rename = execa("npx", ["npe", "name", options.folderName], {
140-
cwd: options.targetDirectory,
141-
});
109+
execa('npx', ['npe','name',options.folderName], {
110+
cwd: options.targetDirectory
111+
}).stdout?.pipe(process.stdout);
112+
})
142113

143-
rename.stdout?.pipe(process.stdout);
144-
});
145-
} catch (err) {
146-
console.error(
147-
`\n%s Template name or directory path is (probably) incorrect`,
148-
chalk.red.bold("ERROR")
149-
);
114+
}catch (err) {
115+
console.error(`\n%s Template name or directory path is (probably) incorrect`, chalk.red.bold('ERROR'));
150116
userSupport();
151117
process.exit(1);
152118
}
@@ -159,32 +125,22 @@ export let downloadTemplateKit = async (options: {
159125

160126
const listrTasks = new Listr([
161127
{
162-
title: `${chalk.green(
163-
`${options.template} template`
164-
)} copied into the generated folder ${chalk.green(
165-
`=> ${options.folderName}`
166-
)}`,
167-
task: () => copyTemplateFolderContent(options),
128+
title: `${chalk.green(`${options.template} template`)} copied into the generated folder ${chalk.green(`=> ${options.folderName}`)}`,
129+
task: () => copyTemplateFolderContent(options)
168130
},
169131
{
170-
title: "git init",
132+
title: 'git init',
171133
task: () => gitInit(options),
172-
skip: () =>
173-
!options.git
174-
? "Skipped because you specified either --skip-git or --yes flags"
175-
: undefined,
134+
skip: () => !options.git ? 'Skipped because you specified either --skip-git or --yes flags' : undefined
176135
},
177136
{
178-
title: "npm install",
137+
title: 'npm install',
179138
task: () => npmInstall(options),
180-
skip: () =>
181-
!options.runInstall
182-
? "Skipped because you specified either --skip-install or --yes flags"
183-
: undefined,
184-
},
139+
skip: () => !options.runInstall ? 'Skipped because you specified either --skip-install or --yes flags' : undefined
140+
}
185141
]);
186142

187143
await listrTasks.run();
188144

189145
return true;
190-
};
146+
}

src/prompts/template.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inquirer from 'inquirer';
22
import chalk from 'chalk';
3+
import { TemplateQuestions } from '../interfaces';
34

45
let skipPromptsModified = (options: any, defaultFolderName: string, notAmongTemplateCollection: any, defaultTemplate: string) => {
56
if (notAmongTemplateCollection && (options.template !== undefined || options.template === undefined)) {

0 commit comments

Comments
 (0)