-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·149 lines (130 loc) · 3.47 KB
/
index.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
141
142
143
144
145
146
147
148
149
#!/usr/bin/env node
import prompts from 'prompts'
import fs from 'node:fs'
import path from 'node:path'
import url from 'node:url'
const TPL_DIR = path.join(path.dirname(url.fileURLToPath(import.meta.url)), "tpl");
const { projectName, root, confirmation } = await prompts([
{
type: 'text',
name: 'projectName',
message: 'Enter a project\'s name',
initial: "ae-contract"
},
{
type: "text",
name: 'root',
initial: prev => projectNameToPath(prev),
message: 'Enter a path'
},
{
type: 'confirm',
name: 'confirmation',
message: `Is this OK?`,
initial: true
}
]);
if (!confirmation) {
console.log("Not OK. Aborting.")
process.exit(-1)
}
createRoot(root)
createGitIgnore(root)
createPackageJSON(root, projectName)
createAssemblyDir(root)
createTestsDir(root)
createConfigFiles(root)
console.log(`
Done. Now run:
cd ${root}
npm install
npm test
`)
process.exit(0)
///
///
///
///
///
///
function createRoot(root) {
if (fs.existsSync(root)) {
console.log("Path already existing. Aborting.")
process.exit(-1)
} else {
fs.mkdirSync(root, { recursive: true });
}
}
function createGitIgnore(root) {
fs.writeFileSync(
path.join(root, ".gitignore"),
["node_modules", ".DS_Store", "dist"].join("\n"),
);
}
function createPackageJSON(root, projectName) {
fs.writeFileSync(
path.join(root, "package.json"),
JSON.stringify({
"name": path.basename(root),
"description": projectName,
"version": "0.1.0",
"type": "module",
"scripts": {
"build": "aewasm build",
"test": "npm run build && vitest",
},
"dependencies": {
"@archethicjs/ae-contract-as": "^1.0.0",
"@archethicjs/ae-contract-test": "^1.0.0",
"assemblyscript": "^0.27.29"
},
"devDependencies": {
"visitor-as": "^0.11.4",
"@types/node": "^22.1.0",
"vitest": "^2.1.1",
"typescript": "^5.5.4"
},
"overrides": {
"assemblyscript": "$assemblyscript"
}
}, null, 2),
);
}
function createAssemblyDir(root) {
fs.mkdirSync(path.join(root, "assembly"))
fs.writeFileSync(
path.join(root, "assembly", "index.ts"),
fs.readFileSync(path.join(TPL_DIR, "assembly", "index.ts")),
);
fs.writeFileSync(
path.join(root, "assembly", "tsconfig.json"),
fs.readFileSync(path.join(TPL_DIR, "assembly", "tsconfig.json")),
);
}
function createTestsDir(root) {
fs.mkdirSync(path.join(root, "tests"))
fs.writeFileSync(
path.join(root, "tests", "index.test.ts"),
fs.readFileSync(path.join(TPL_DIR, "tests", "index.test.ts")),
);
}
function createConfigFiles(root) {
fs.writeFileSync(
path.join(root, "asconfig.json"),
fs.readFileSync(path.join(TPL_DIR, "asconfig.json")),
);
fs.writeFileSync(
path.join(root, "tsconfig.json"),
fs.readFileSync(path.join(TPL_DIR, "tsconfig.json")),
);
}
function projectNameToPath(projectName) {
return path.join(process.cwd(), kebabCase(projectName)).toString()
}
function kebabCase(string) {
return string
.trim()
.replace(/([a-z])([A-Z])/g, "$1-$2")
.replace(/[\s_]+/g, '-')
.toLowerCase();
}