-
-
Notifications
You must be signed in to change notification settings - Fork 708
Expand file tree
/
Copy pathwriteCommonJsPackageJson.mjs
More file actions
43 lines (34 loc) · 801 Bytes
/
Copy pathwriteCommonJsPackageJson.mjs
File metadata and controls
43 lines (34 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env node
import fs from 'node:fs/promises';
import { argv } from 'node:process';
import path from 'node:path';
import { writeFile } from 'node:fs/promises';
/**
* @param {string} path
* @returns {Promise<boolean>}
*/
async function pathExists(path) {
try {
await fs.access(path);
return true;
} catch (_err) {
return false;
}
}
const directory = argv[2];
if (directory === undefined) {
throw new Error('Expected a path');
}
const directoryExists = await pathExists(directory);
if (!directoryExists) {
throw new Error(`Path ${directory} not found`);
}
const filePath = path.join(directory, 'package.json');
const packageJsonFileContent = JSON.stringify(
{
type: 'commonjs',
},
undefined,
2,
);
await writeFile(filePath, packageJsonFileContent);