ts-init-template 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,106 @@
1
+ import { build } from 'esbuild';
2
+ import readline from 'readline/promises';
3
+ import { stdin as input, stdout as output } from 'process';
4
+ import fg from 'fast-glob';
5
+ import fs from 'fs/promises';
6
+ import path from 'path';
7
+
8
+ const rl = readline.createInterface({ input, output });
9
+
10
+ // get the files and folders in src folder
11
+ const srcPath = path.join(import.meta.dirname, 'src');
12
+
13
+ // Check if the src directory exists
14
+ try{
15
+ await fs.access(srcPath);
16
+ } catch {
17
+ // Create the src directory if it does not exist
18
+ console.log('The src directory does not exist, Creating it now...');
19
+ await fs.mkdir(srcPath, { recursive: true });
20
+ console.log('✅ src directory created successfully. You can now add your source files.');
21
+ process.exit(0);
22
+ };
23
+
24
+ const srcFiles = await fg('src/**/*', { onlyFiles: true });
25
+ const srcFolders = await fg('src/**/', { onlyDirectories: true });
26
+
27
+ if (srcFiles.length === 0 && srcFolders.length === 0) {
28
+ console.log('⚠️ No files found in the src directory.')
29
+ const answer = await rl.question('Do you want to remove all files in dist? (y/N): ');
30
+ if (answer.toLowerCase() === 'y') {
31
+ console.log('Removing all files in dist directory...');
32
+ // Check if the dist directory exists
33
+ const distFiles = await fg('dist/**/*', { onlyFiles: true });
34
+ await Promise.all(
35
+ distFiles.map(file => fs.rm(file, { force: true }))
36
+ );
37
+ console.log('✅ All files in dist directory removed.');
38
+ rl.close();
39
+ process.exit(0);
40
+ } else {
41
+ console.log('❌ Operation canceled.');
42
+ rl.close();
43
+ process.exit(0);
44
+ }
45
+ };
46
+
47
+ // generate lists of files and folders from src to dist folder
48
+ // src -> dist
49
+ const validDistFiles = srcFiles.flatMap((f) => {
50
+ const distBase = f.replace(/^src/, 'dist').replace(/\.ts$/, '');
51
+ return [`${distBase}.js`, `${distBase}.js.map`];
52
+ });
53
+ const validDistFolders = srcFolders.map(f => f.replace(/^src/, 'dist'));
54
+
55
+ // delete the files which were deleted in src from dist folder
56
+ const distFiles = await fg('dist/**/*', { onlyFiles: true });
57
+
58
+ for (const file of distFiles) {
59
+ if (!validDistFiles.includes(file)) {
60
+ await fs.rm(file, { force: true });
61
+ console.log(`🗑️ Removed obsolete file: ${file}`);
62
+ }
63
+ };
64
+
65
+ // delete the folders which were deleted in src from dist folder
66
+ const distFolders = await fg('dist/**/', { onlyDirectories: true });
67
+ for (const folder of distFolders) {
68
+ if (!validDistFolders.includes(folder)) {
69
+ await fs.rm(folder, { recursive: true, force: true });
70
+ console.log(`🗑️ Removed obsolete folder: ${folder}`);
71
+ }
72
+ };
73
+
74
+ // build ts files
75
+ const entryPoints = await fg('src/**/*.ts');
76
+
77
+ const buildts = await build({
78
+ entryPoints,
79
+ outdir: 'dist',
80
+ bundle: false,
81
+ platform: 'node',
82
+ target: ['ESNext'],
83
+ format: 'esm',
84
+ logLevel: 'info',
85
+ metafile: true,
86
+ });
87
+
88
+ for (const file in buildts.metafile.outputs) {
89
+ console.log(`📦 Built file: ${file}`);
90
+ };
91
+
92
+ // add empty folders from src to dist folder
93
+ for (const folder of srcFolders) {
94
+ const distFolder = folder.replace(/^src/, 'dist');
95
+
96
+ try{
97
+ await fs.access(distFolder);
98
+ }catch{
99
+ // Create the directory in dist if it does not exist
100
+ await fs.mkdir(distFolder, { recursive: true });
101
+ console.log(`📁 Created folder: ${distFolder}`);
102
+ };
103
+ };
104
+
105
+ console.log('✅ Build script completed.');
106
+ process.exit(0);
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node",
6
+ "outDir": "dist",
7
+ "rootDir": "src",
8
+ "esModuleInterop": true,
9
+ "forceConsistentCasingInFileNames": true,
10
+ "strict": true,
11
+ "skipLibCheck": true,
12
+ "resolveJsonModule": true
13
+ },
14
+ "include": ["src/**/*"],
15
+ "exclude": ["node_modules", "dist"]
16
+ }
package/index.js ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { exec } from 'child_process';
4
+ import { promisify } from 'util';
5
+ import * as fs from 'fs/promises';
6
+ import path from 'path';
7
+ const __dirname = import.meta.dirname
8
+
9
+ const execAsync = promisify(exec);
10
+ //install dependencies
11
+ try{
12
+ const { stdout, stderr } = await execAsync('yarn add -D typescript ts-node @types/node fast-glob esbuild');
13
+ console.log('Dependencies installed successfully:', stdout);
14
+ if(stderr){
15
+ console.error('Error during installation:', stderr);
16
+ };
17
+ } catch (error) {
18
+ console.error('❌ Failed to install dependencies:', error);
19
+ process.exit(1);
20
+ };
21
+
22
+
23
+ // Check if package.json exists
24
+ const pkgpath = path.join(__dirname, 'package.json')
25
+ try {
26
+ await fs.access(pkgpath);
27
+ console.log('package.json exists, proceeding with script execution.');
28
+
29
+ console.log(pkgpath);
30
+ // Read and parse package.json
31
+ const content = await fs.readFile(pkgpath, 'utf8');
32
+ const pkg = JSON.parse(content);
33
+ pkg.type = 'module';
34
+ pkg.scripts = {
35
+ ...pkg.scripts,
36
+ scripts: {
37
+ build: "node build.js",
38
+ start: "node dist/index.js"
39
+ },
40
+ }
41
+
42
+ // Add or update the package.json properties
43
+ await fs.writeFile(pkgpath, JSON.stringify(pkg, null, 2));
44
+ console.log('✅ package.json updated.');
45
+
46
+ }
47
+ catch (error) {
48
+ console.error('❌ package.json not found:', error);
49
+ process.exit(1);
50
+ };
51
+
52
+ try{
53
+ await fs.copyFile('addfiles/build.js', path.join(__dirname, 'build.js'))
54
+ await fs.copyFile('addfiles/tsconfig.json', path.join(__dirname, 'tsconfig.json'));
55
+ console.log('Files "build.js" and "tsconfig.json" copied successfully');
56
+ } catch (error) {
57
+ console.error('❌ Failed to copy files:', error);
58
+ process.exit(1);
59
+ }
60
+
61
+ console.log('✅ All operations completed successfully!');
62
+ process.exit(0);
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "ts-init-template",
3
+ "version": "1.0.0",
4
+ "description": "A ts project template for my ts projects. This script will setup all the ts enviroment with 1 command. ",
5
+ "main": "index.js",
6
+ "author": "SigismundBT",
7
+ "license": "MIT",
8
+ "dependencies": {},
9
+ "devDependencies": {},
10
+ "type": "module",
11
+ "bin": {
12
+ "ts-init": "./index.js"
13
+ }
14
+ }