This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.ts
154 lines (122 loc) · 3.37 KB
/
generate.ts
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
150
151
152
153
154
/**
* @fileoverview Definitions Generator
*/
//Imports
import {execSync} from 'child_process';
import {existsSync, mkdirSync, readdirSync, renameSync, rmSync, writeFileSync} from 'fs';
import {join} from 'path';
//If definitions already exist, remove it
if (existsSync('src/definitions'))
{
rmSync('src/definitions', {
recursive: true
});
console.log('❌ Removing existing definitions!');
}
//Make definitions directory
mkdirSync('src/definitions');
//Clone
execSync('git clone --depth 1 --filter=blob:none --no-checkout https://github.com/Ultimaker/Cura.git upstream');
//Checkout
execSync('git sparse-checkout init --cone', {
cwd: 'upstream'
});
execSync('git sparse-checkout add resources/definitions', {
cwd: 'upstream'
});
execSync('git sparse-checkout add resources/extruders', {
cwd: 'upstream'
});
execSync('git checkout', {
cwd: 'upstream'
});
console.log('⬇ Cloned 3D definitions!');
//Get definitions
const rawExtruders = readdirSync('upstream/resources/extruders');
const rawPrinters = readdirSync('upstream/resources/definitions');
//Move/merge definitions
for (const definition of rawExtruders)
{
//Move to "src/definitions"
renameSync(join('upstream/resources/extruders', definition), join('src/definitions', definition.toLowerCase()));
}
for (const definition of rawPrinters)
{
//Move to "src/definitions"
renameSync(join('upstream/resources/definitions', definition), join('src/definitions', definition.toLowerCase()));
}
//Remove cloned repository
rmSync('upstream', {
recursive: true
});
console.log('📂 Moved definitions!');
/**
* Normalize a name
* @param name The name to normalize
*/
const normalize = (name: string) =>
{
//If the name starts with a number, append an underscore
if (name.length > 0 && /\d/.test(name.charAt(0)))
{
name = `_${name}`;
}
//Replace hyphens with underscores
name = name.replace('-', '_');
name = name.toLowerCase();
//Remove file extensions
const matches = /(.+)\.def\.json/.exec(name);
if (matches != null)
{
return matches[1];
}
else
{
throw new Error(`Unable to normalize ${name}`);
}
};
//Normalize definitions
const extruders = rawExtruders.map(extruder => ({
normalized: normalize(extruder),
raw: extruder.toLowerCase()
}));
const printers = rawPrinters.map(printer => ({
normalized: normalize(printer),
raw: printer.toLowerCase()
}));
//Generate import statements
const imports = printers
.concat(extruders)
.map(definition => `import * as ${definition.normalized} from './${definition.raw}';`)
.join('\r\n');
//Generate export statements
const extruderExports = extruders
.map(extruder => ` ${extruder.normalized},`)
.join('\r\n');
const printerExports = printers
.map(printer => ` ${printer.normalized},`)
.join('\r\n');
//Create the file template
const template = `/* eslint-disable camelcase */
/**
* @fileoverview Cura Definitions
*
* See https://github.com/Ultimaker/Cura/tree/master/resources/definitions
* for more information
*
* **THIS FILE IS MIT LICENSED!**
*/
//Imports
${imports}
//Export extruder definitions
export const extruders = {
${extruderExports}
};
//Export printer definitions
export const printers = {
${printerExports}
};
`;
//Write the file
writeFileSync(join(__dirname, 'src/definitions/index.ts'), template);
console.log(`📝 Generated index for ${printers.length} printers and ${extruders.length} extruders!`);