@ast-grep/create-lang 0.0.0-prerelease → 0.0.0-prerelease-2
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.
- package/README.md +10 -0
- package/index.js +12 -6
- package/package.json +10 -2
- package/template/.gitignore +30 -0
- package/template/.npmignore +0 -0
- package/template/package.json +1 -1
- package/index.d.ts +0 -1
- package/index.ts +0 -126
- package/tsconfig.json +0 -3
package/README.md
ADDED
package/index.js
CHANGED
|
@@ -61,7 +61,11 @@ function askConfiguration() {
|
|
|
61
61
|
message: 'Include gitignore and npm publish files?',
|
|
62
62
|
initial: true,
|
|
63
63
|
}
|
|
64
|
-
]
|
|
64
|
+
], {
|
|
65
|
+
onCancel: () => {
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
65
69
|
}
|
|
66
70
|
function copyTemplate(targetDir, includeDotFiles) {
|
|
67
71
|
const templateDir = node_path_1.default.join(__dirname, 'template');
|
|
@@ -97,13 +101,15 @@ async function renameFiles(dir, answer) {
|
|
|
97
101
|
}
|
|
98
102
|
}
|
|
99
103
|
}
|
|
100
|
-
function installTreeSitterPackage(answer) {
|
|
104
|
+
function installTreeSitterPackage(cwd, answer) {
|
|
101
105
|
console.log('Installing tree-sitter package...');
|
|
102
|
-
(0, node_child_process_1.execSync)(`pnpm install ${answer.treeSitterPackage} --save-dev --save-exact
|
|
106
|
+
(0, node_child_process_1.execSync)(`pnpm install ${answer.treeSitterPackage} --save-dev --save-exact`, {
|
|
107
|
+
cwd,
|
|
108
|
+
});
|
|
103
109
|
console.log('Copying source code...');
|
|
104
|
-
(0, node_child_process_1.execSync)('pnpm run source');
|
|
110
|
+
(0, node_child_process_1.execSync)('pnpm run source', { cwd });
|
|
105
111
|
console.log('Compiling');
|
|
106
|
-
(0, node_child_process_1.execSync)('pnpm run build');
|
|
112
|
+
(0, node_child_process_1.execSync)('pnpm run build', { cwd });
|
|
107
113
|
}
|
|
108
114
|
async function main() {
|
|
109
115
|
let cwd = process.cwd();
|
|
@@ -114,6 +120,6 @@ async function main() {
|
|
|
114
120
|
const config = await askConfiguration();
|
|
115
121
|
await copyTemplate(cwd, config.includeDotFiles);
|
|
116
122
|
await renameFiles(cwd, config);
|
|
117
|
-
installTreeSitterPackage(config);
|
|
123
|
+
installTreeSitterPackage(cwd, config);
|
|
118
124
|
}
|
|
119
125
|
main();
|
package/package.json
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ast-grep/create-lang",
|
|
3
|
-
"version": "0.0.0-prerelease",
|
|
3
|
+
"version": "0.0.0-prerelease-2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "index.js",
|
|
7
|
-
"keywords": [
|
|
7
|
+
"keywords": [
|
|
8
|
+
"ast-grep"
|
|
9
|
+
],
|
|
8
10
|
"author": "",
|
|
9
11
|
"license": "ISC",
|
|
10
12
|
"bin": {
|
|
11
13
|
"create-lang": "index.js"
|
|
12
14
|
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"template/.gitignore",
|
|
18
|
+
"template/.npmignore",
|
|
19
|
+
"template"
|
|
20
|
+
],
|
|
13
21
|
"dependencies": {
|
|
14
22
|
"prompts": "2.4.2"
|
|
15
23
|
},
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Logs
|
|
2
|
+
logs
|
|
3
|
+
*.log
|
|
4
|
+
npm-debug.log*
|
|
5
|
+
yarn-debug.log*
|
|
6
|
+
yarn-error.log*
|
|
7
|
+
pnpm-debug.log*
|
|
8
|
+
lerna-debug.log*
|
|
9
|
+
|
|
10
|
+
node_modules
|
|
11
|
+
dist
|
|
12
|
+
dist-ssr
|
|
13
|
+
*.local
|
|
14
|
+
|
|
15
|
+
# Editor directories and files
|
|
16
|
+
.vscode/*
|
|
17
|
+
!.vscode/extensions.json
|
|
18
|
+
.idea
|
|
19
|
+
.DS_Store
|
|
20
|
+
*.suo
|
|
21
|
+
*.ntvs*
|
|
22
|
+
*.njsproj
|
|
23
|
+
*.sln
|
|
24
|
+
*.sw?
|
|
25
|
+
|
|
26
|
+
# ignore generated parser files
|
|
27
|
+
parser.so
|
|
28
|
+
type.d.ts
|
|
29
|
+
# do not include copied directories
|
|
30
|
+
src
|
|
File without changes
|
package/template/package.json
CHANGED
package/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/index.ts
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import prompts from 'prompts'
|
|
2
|
-
import path from 'node:path'
|
|
3
|
-
import fs from 'node:fs/promises'
|
|
4
|
-
import { execSync } from 'node:child_process'
|
|
5
|
-
|
|
6
|
-
function required(s: string): string | true {
|
|
7
|
-
if (s.length === 0) {
|
|
8
|
-
return 'This value is required'
|
|
9
|
-
}
|
|
10
|
-
return true
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// https://github.com/vitejs/vite/blob/76082e3d3033b09b02b6db64de6e36942593c753/packages/create-vite/src/index.ts#L557
|
|
14
|
-
function isValidPackageName(projectName: string) {
|
|
15
|
-
return /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(
|
|
16
|
-
projectName,
|
|
17
|
-
) || 'Invalid package name'
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
function askConfiguration() {
|
|
22
|
-
return prompts([
|
|
23
|
-
{
|
|
24
|
-
type: 'text',
|
|
25
|
-
name: 'name',
|
|
26
|
-
message: 'Language name',
|
|
27
|
-
validate: required,
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
type: 'text',
|
|
31
|
-
name: 'packageName',
|
|
32
|
-
message: 'Package name',
|
|
33
|
-
validate: isValidPackageName,
|
|
34
|
-
initial: (_, answers) => `my-dynamic-lang-${answers.name}`,
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
type: 'text',
|
|
38
|
-
name: 'treeSitterPackage',
|
|
39
|
-
message: 'Tree-sitter package to use',
|
|
40
|
-
validate: isValidPackageName,
|
|
41
|
-
initial: (_, answers) => `tree-sitter-${answers.name}`,
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
type: 'list',
|
|
45
|
-
name: 'extensions',
|
|
46
|
-
message: 'File extensions used by the language, comma separated',
|
|
47
|
-
separator: ',',
|
|
48
|
-
validate: required,
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
type: 'text',
|
|
52
|
-
name: 'expandoChar',
|
|
53
|
-
message: 'Expando char used in pattern',
|
|
54
|
-
initial: '$',
|
|
55
|
-
validate: (value) => {
|
|
56
|
-
return value.length === 1 ? true : 'Expando char must be a single character'
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
type: 'confirm',
|
|
61
|
-
name: 'includeDotFiles',
|
|
62
|
-
message: 'Include gitignore and npm publish files?',
|
|
63
|
-
initial: true,
|
|
64
|
-
}
|
|
65
|
-
])
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
type Answers = Awaited<ReturnType<typeof askConfiguration>>
|
|
69
|
-
|
|
70
|
-
function copyTemplate(targetDir: string, includeDotFiles: boolean) {
|
|
71
|
-
const templateDir = path.join(__dirname, 'template')
|
|
72
|
-
return fs.cp(templateDir, targetDir, {
|
|
73
|
-
recursive: true, // Copy all files and folders
|
|
74
|
-
// includes hidden files if `includeDotFiles` is true
|
|
75
|
-
filter: (src) => {
|
|
76
|
-
const basename = path.basename(src)
|
|
77
|
-
return includeDotFiles || !basename.startsWith('.')
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
async function renameFiles(dir: string, answer: Answers) {
|
|
83
|
-
const name: Record<string, string> = {
|
|
84
|
-
$$PACKAGE_NAME$$: answer.packageName,
|
|
85
|
-
$$NAME$$: answer.name,
|
|
86
|
-
$$TREE_SITTER_PACKAGE$$: answer.treeSitterPackage,
|
|
87
|
-
$$EXTENSIONS$$: JSON.stringify(answer.extensions),
|
|
88
|
-
$$EXPANDO_CHAR$$: answer.expandoChar,
|
|
89
|
-
}
|
|
90
|
-
for (const file of await fs.readdir(dir)) {
|
|
91
|
-
const filePath = path.join(dir, file)
|
|
92
|
-
const stats = await fs.stat(filePath)
|
|
93
|
-
if (stats.isDirectory()) {
|
|
94
|
-
renameFiles(filePath, answer)
|
|
95
|
-
} else {
|
|
96
|
-
const content = await fs.readFile(filePath, 'utf-8')
|
|
97
|
-
const newContent = content.replace(/(\$\$[A-Z_]+\$\$)/g, (match) => {
|
|
98
|
-
return name[match] || match
|
|
99
|
-
})
|
|
100
|
-
await fs.writeFile(filePath, newContent)
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
function installTreeSitterPackage(answer: Answers) {
|
|
105
|
-
console.log('Installing tree-sitter package...')
|
|
106
|
-
execSync(`pnpm install ${answer.treeSitterPackage} --save-dev --save-exact`)
|
|
107
|
-
console.log('Copying source code...')
|
|
108
|
-
execSync('pnpm run source')
|
|
109
|
-
console.log('Compiling')
|
|
110
|
-
execSync('pnpm run build')
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async function main() {
|
|
114
|
-
let cwd = process.cwd()
|
|
115
|
-
if (process.argv.length > 2) {
|
|
116
|
-
const targetDir = process.argv[2]
|
|
117
|
-
cwd = path.join(cwd, targetDir)
|
|
118
|
-
}
|
|
119
|
-
const config = await askConfiguration()
|
|
120
|
-
await copyTemplate(cwd, config.includeDotFiles)
|
|
121
|
-
await renameFiles(cwd, config)
|
|
122
|
-
installTreeSitterPackage(config)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
main()
|
|
126
|
-
|
package/tsconfig.json
DELETED