Skip to content

Commit 9d882d6

Browse files
committed
Fixed a bug where package.json would not be detected and you can now only use the typescript-template flag instead of using both typescript flags
1 parent 0527063 commit 9d882d6

1 file changed

Lines changed: 61 additions & 57 deletions

File tree

index.js

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,43 @@ parser.add_argument('--yarn', {
2828
action: 'store_true',
2929
});
3030

31-
const { dir, template, typescript, typescriptTemplate, yarn } =
31+
const { dir, template, typescript, typescript_template, yarn } =
3232
parser.parse_args();
3333
const path = `${dir}/package.json`;
3434

35-
const readFile = async () => {
36-
console.log('Reading package.json...');
35+
async function readFile() {
36+
console.log('Reading package.json & prettier template...');
3737
const packageJsonPromise = fs
3838
.readFile(path)
39-
.then(val => {
40-
console.log('Successfully read package.json');
41-
return JSON.parse(val);
42-
})
39+
.then(val => val.toString('utf-8'))
40+
.then(val => JSON.parse(val))
4341
.catch(err => {
4442
console.log(
4543
'An error occurred while reading the package.json',
4644
err
4745
);
48-
process.abort();
46+
process.exit(1);
4947
});
50-
console.log('Reading template file...');
5148
const templatePromise = fs
5249
.readFile(template)
53-
.then(val => {
54-
console.log('Finished reading template file...');
55-
return JSON.parse(val);
56-
})
57-
.catch(() => console.log('No template file could be read'));
50+
.then(val => val.toString('utf-8'))
51+
.then(JSON.parse)
52+
.catch(() => console.log('No prettier template file could be read'));
5853
const [packageJSON, templateJSON] = await Promise.all([
5954
packageJsonPromise,
6055
templatePromise,
6156
]);
6257

6358
return [packageJSON, templateJSON];
64-
};
59+
}
6560

66-
const writeFile = async data => {
61+
async function writeFile(data) {
6762
console.log('Writing to package.json...');
6863
await fs.writeFile(path, data);
69-
};
64+
}
7065

71-
const typescriptConfig = async packageJSON => {
72-
if (typescript) {
66+
async function typescriptConfig(packageJSON) {
67+
if (typescript || typescript_template) {
7368
if (
7469
!Object.keys(packageJSON?.dependencies || {}).includes(
7570
'typescript'
@@ -87,51 +82,60 @@ const typescriptConfig = async packageJSON => {
8782
install.on('exit', () =>
8883
console.log('Successfully installed typescript')
8984
);
90-
if (typescriptTemplate) {
91-
console.log('Copying Template tsconfig file...');
92-
await fs.copyFile(typescriptTemplate, `${dir}/tsconfig.json`);
93-
console.log('Copied Tsconfig file');
94-
} else {
95-
console.log('Writing tsconfig file...');
96-
await fs.writeFile(
97-
`${dir}/tsconfig.json`,
98-
JSON.stringify({
99-
compilerOptions: {
100-
target: 'es5',
101-
lib: ['dom', 'dom.iterable', 'esnext'],
102-
allowJs: true,
103-
skipLibCheck: true,
104-
esModuleInterop: true,
105-
allowSyntheticDefaultImports: true,
106-
strict: true,
107-
forceConsistentCasingInFileNames: true,
108-
noFallthroughCasesInSwitch: true,
109-
module: 'esnext',
110-
moduleResolution: 'node',
111-
resolveJsonModule: true,
112-
isolatedModules: true,
113-
noEmit: true,
114-
noImplicitAny: true,
115-
noImplicitReturns: true,
116-
jsx: 'react-jsx',
117-
},
118-
include: ['./src/'],
119-
})
120-
);
121-
console.log('Finished Writing tsconfig file');
122-
}
123-
await exec.exec(`prettier --write ${dir}/tsconfig.json`);
12485
}
86+
if (typescript_template) {
87+
console.log('Copying Template tsconfig file...');
88+
await fs
89+
.copyFile(typescript_template, `${dir}/tsconfig.json`)
90+
.catch(err => {
91+
console.error("Couldn't copy tsconfig.json file", err);
92+
process.exit(1);
93+
});
94+
console.log('Copied Tsconfig file');
95+
} else {
96+
console.log('Writing tsconfig file...');
97+
await fs.writeFile(
98+
`${dir}/tsconfig.json`,
99+
JSON.stringify({
100+
compilerOptions: {
101+
target: 'es5',
102+
lib: ['dom', 'dom.iterable', 'esnext'],
103+
allowJs: true,
104+
skipLibCheck: true,
105+
esModuleInterop: true,
106+
allowSyntheticDefaultImports: true,
107+
strict: true,
108+
forceConsistentCasingInFileNames: true,
109+
noFallthroughCasesInSwitch: true,
110+
module: 'esnext',
111+
moduleResolution: 'node',
112+
resolveJsonModule: true,
113+
isolatedModules: true,
114+
noEmit: true,
115+
noImplicitAny: true,
116+
noImplicitReturns: true,
117+
jsx: 'react-jsx',
118+
},
119+
include: ['./src/'],
120+
})
121+
);
122+
}
123+
console.log('Finished Writing tsconfig file');
124+
await exec.exec(`prettier --write ${dir}/tsconfig.json`);
125125
}
126-
};
126+
}
127127

128-
const writePrettierSettings = async () => {
128+
async function writePrettierSettings() {
129129
await fs.access(`${dir}/package.json`).catch(() => {
130130
console.error('Cannot access package.json');
131131
process.exit(1);
132132
});
133133
let [packageJSON, templateJSON] = await readFile();
134-
await typescriptConfig();
134+
console.log(
135+
'Successfully read the package.json file',
136+
templateJSON ? 'Successfully read the prettier template file' : ''
137+
);
138+
await typescriptConfig(packageJSON);
135139
await writeFile(
136140
JSON.stringify({
137141
...packageJSON,
@@ -152,6 +156,6 @@ const writePrettierSettings = async () => {
152156
console.log('Finished writing to package.json \nFormatting...');
153157
await exec.exec(`prettier --write ${dir}/package.json`);
154158
console.log('Finished formatting.');
155-
};
159+
}
156160

157161
writePrettierSettings().then(() => console.log('Done.'));

0 commit comments

Comments
 (0)