-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupNpmLib.js
140 lines (123 loc) · 3.88 KB
/
setupNpmLib.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import fs from 'fs';
import path from 'path';
import commentJson from 'comment-json';
import { generateIndexFile } from './handleExport.js';
let forceCreateIndexTs = false;
const PACKAGE_JSON_PATH = path.join(process.cwd(), 'package.json');
const packageJsonData = fs.readFileSync(PACKAGE_JSON_PATH, 'utf-8');
const packageJson = commentJson.parse(packageJsonData);
packageJson.main = `dist/${packageJson.name}.js`;
packageJson.types = "dist/index.d.ts";
packageJson.files = [
"dist/**/*.js",
"dist/**/*.d.ts"
];
fs.writeFileSync(PACKAGE_JSON_PATH, commentJson.stringify(packageJson, null, 2));
const TSCONFIG_PATH = path.join(process.cwd(), 'tsconfig.json');
const tsconfigData = fs.readFileSync(TSCONFIG_PATH, 'utf-8');
const tsconfig = commentJson.parse(tsconfigData);
tsconfig.compilerOptions.outDir = "dist";
tsconfig.compilerOptions.rootDir = "src";
tsconfig.compilerOptions.declarationDist = "dist";
tsconfig.compilerOptions.declaration = true;
tsconfig.compilerOptions.resolveJsonModule = true;
tsconfig.compilerOptions.skipLibCheck = true;
tsconfig.compilerOptions.noEmit = false;
tsconfig.compilerOptions.useDefineForClassFields = true;
tsconfig.compilerOptions.allowImportingTsExtensions = undefined;
tsconfig.compilerOptions.declarationDist = undefined;
if (!tsconfig.compilerOptions.typeRoots) tsconfig.compilerOptions.typeRoots = [];
if (tsconfig.compilerOptions.typeRoots.indexOf("./node_modules/@types") === -1) {
tsconfig.compilerOptions.typeRoots.push("./node_modules/@types");
}
if (!tsconfig.include) tsconfig.include = [];
if (tsconfig.include.indexOf("src") === -1) tsconfig.include.push("src");
if (tsconfig.include.indexOf("index.ts") === -1) tsconfig.include.push("index.ts");
fs.writeFileSync(TSCONFIG_PATH, commentJson.stringify(tsconfig, null, 2));
const SRC_PATH = path.join(process.cwd(), 'src');
const indexts = path.join(SRC_PATH, "index.ts");
if (!fs.existsSync(indexts) || forceCreateIndexTs) {
generateIndexFile(SRC_PATH);
console.log("create src/index.ts");
}
const envFilePath = path.join(process.cwd(), '.env');
if (!fs.existsSync(envFilePath)) {
fs.writeFileSync(envFilePath, 'VITE_FOOBAR = 1234', 'utf-8');
console.log('create .env file');
}
const viteEnvFilePath = path.join(process.cwd(), 'vite-env.d.ts');
if (!fs.existsSync(viteEnvFilePath)) {
fs.writeFileSync(viteEnvFilePath, `
interface ImportMeta {
env: {
[key: string]: string;
};
}
`, 'utf-8');
} else {
let viteEnv = fs.readFileSync(viteEnvFilePath, 'utf-8');
if (!viteEnv.includes("interface ImportMeta")) {
viteEnv = viteEnv + `
interface ImportMeta {
env: {
[key: string]: string;
};
}
`;
fs.writeFileSync(viteEnvFilePath, viteEnv, 'utf-8');
}
}
const viteConfigFilePath = path.join(process.cwd(), 'vite.config.js');
if (!fs.existsSync(viteConfigFilePath)) {
fs.writeFileSync(viteConfigFilePath, `
// vite.config.js
import { defineConfig } from 'vite'
import Dts from 'vite-plugin-dts';
export default defineConfig({
plugins: [Dts()],
build: {
minify: false,
lib: {
entry: 'src/index.ts',
name: '${packageJson.name}',
fileName: '${packageJson.name}',
},
},
})
`, 'utf-8');
}
const gitFilePath = path.join(process.cwd(), '.gitignore');
if (fs.existsSync(gitFilePath)) {
const gitignoreContent = fs.readFileSync(gitFilePath, 'utf-8');
if (!gitignoreContent.includes(".env")) {
const updatedContent = gitignoreContent + `
.env
`;
fs.writeFileSync(gitFilePath, updatedContent, 'utf-8');
}
} else {
fs.writeFileSync(gitFilePath, `
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.env
`, 'utf-8');
}