-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.config.js
98 lines (75 loc) · 2.98 KB
/
build.config.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { execSync } from 'child_process';
import fs, { cpSync } from 'fs';
import path from 'path';
/**
* @typedef {{
* version: string,
* private?: string | boolean,
* main: string,
* types: string,
* scripts?: Record<string, string>,
* publishConfig: {
* access: string
* },
* }} PackageJson
*/
const ROOT_PROJECT = process.cwd();
const outDirName = 'dist';
buildPackageConfig();
async function buildPackageConfig() {
cleanDistDirectory();
buildWithVite();
copyStaticFiles();
manipulatePackageJsonFile();
console.log('DONE !!!');
}
function cleanDistDirectory() {
console.log('- Step 1: clear the dist directory');
execSync('rm -rf dist');
}
function buildWithVite() {
console.log('- Step 2: build with vite');
execSync('tsc -p ./tsconfig.package.json && vite build --config vite.config.package.ts');
}
function copyStaticFiles() {
console.log('[32m- Step 3:[39m copy static files');
const filesToCopyArr = [
{ filename: 'package.json', sourceDirPath: [], destinationDirPath: [] },
{ filename: '.npmignore', sourceDirPath: [], destinationDirPath: [] },
{ filename: '.npmrc', sourceDirPath: [], destinationDirPath: [], isAllowedToFail: true },
{ filename: 'README.md', sourceDirPath: [], destinationDirPath: [] },
];
filesToCopyArr.forEach(({ filename, sourceDirPath, destinationDirPath, isAllowedToFail }) => {
try {
const sourceFileFullPath = path.resolve(ROOT_PROJECT, ...sourceDirPath, filename);
const destinationFileFullPath = path.resolve(ROOT_PROJECT, outDirName, ...destinationDirPath, filename);
cpSync(sourceFileFullPath, destinationFileFullPath);
console.log(` • ${filename}`);
} catch (error) {
console.error(error);
if (isAllowedToFail) return;
throw new Error('File MUST exists in order to PASS build process! cp operation failed...');
}
});
}
function manipulatePackageJsonFile() {
console.log('[32m- Step 5:[39m copy & manipulate the package.json file');
const packageJsonPath = path.resolve(ROOT_PROJECT, outDirName, 'package.json');
// Step 1: get the original package.json file
/** @type {PackageJson} */
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
// Step 2: Remove all scripts
delete packageJson.scripts;
console.log(' • [34mdeleted[39m `scripts` key');
// Step 3: Change from private to public
delete packageJson.private;
packageJson.publishConfig.access = 'public';
console.log(' • [34mchanged[39m from private to public');
console.log(' • [34mchanged[39m publishConfig access to public');
// Step 4: remove 'outDirName/' from "main" & "types"
packageJson.main = packageJson.main.replace(`${outDirName}/`, '');
packageJson.types = packageJson.types.replace(`${outDirName}/`, '');
// Step 5: create new package.json file in the output folder
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
console.log(' • [34mpackage.json[39m file written successfully!');
}