-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
272 lines (236 loc) · 8.99 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env node
const inquirer = require("inquirer");
const path = require("path");
const fs = require("fs");
const chalk = require("chalk");
const UnderscoreTemplate = require("underscore.template");
var pjson = require('./package.json');
console.log(chalk.bgWhite.black(`Welcome to CREATE-ESP32-APP v${pjson.version} 🎉`))
inquirer.registerPrompt("fuzzypath", require("inquirer-fuzzy-path"));
const CURR_DIR = process.cwd();
const rootPath = process.platform === "win32" ? "c:\\" : require("os").homedir();
let defaultIDFPath;
if (!!process.env.IDF_PATH) {
console.log(chalk.magenta("IDF_PATH env var set! using " + process.env.IDF_PATH));
defaultIDFPath = process.env.IDF_PATH;
} else {
console.log(chalk.magenta("IDF_PATH env var not set"));
defaultIDFPath = path.join(rootPath, "esp", "esp-idf");
}
let defaultExtensiaToolsPath;
if (!!process.env.IDF_TOOLS_PATH) {
console.log(chalk.magenta("IDF_TOOLS_PATH env var set! using " + process.env.IDF_TOOLS_PATH));
defaultExtensiaToolsPath = process.env.IDF_TOOLS_PATH;
} else {
console.log(chalk.magenta("IDF_TOOLS_PATH env var not set"));
defaultExtensiaToolsPath = path.join(rootPath, "esp", "tools", ".espressif");
}
const questions = [
{
name: "projectName",
type: "input",
message: "Project Name",
validate: function (input) {
const folderName = `${CURR_DIR}/${input}`;
if (fs.existsSync(folderName)) {
return "Project name directory already exists. Please use another name";
}
if (/^([A-Za-z\_][A-Za-z\-\_\d]*$)/gm.test(input)) return true;
else return "Project name must be alphanumeric and start with a letter";
}
},
{
name: "iDFPath",
type: "fuzzypath",
validate: function (input) {
if (!!input) return true;
else return "IDF path cannot be blank (press tab to select path)";
},
itemType: "directory",
rootPath: rootPath,
message: "Select directory to ESP-IDF",
default: defaultIDFPath,
suggestOnly: true,
depthLimit: 1
},
{
name: "toolsPath",
type: "fuzzypath",
validate: function (input) {
if (!!input) return true;
else return "IDF tools path cannot be blank (press tab to select path)";
},
itemType: "directory",
rootPath: rootPath,
message: "Select directory to espressif Tools (Xtensa tools[.espressif] directory])",
default: defaultExtensiaToolsPath,
suggestOnly: true,
depthLimit: 1
},
{
name: "isCpp",
type: "confirm",
message: "Use C++ (default is c [press Enter])",
default: false,
}
];
function getAdditions(answers) {
const additionDirs = fs.readdirSync(path.resolve(__dirname, "Additions"));
const additionChoices = additionDirs.map(dir => {
const value = dir;
const additionalData = require(path.resolve(__dirname, "Additions", dir, "template.json"));
const isCpp = !!additionalData.isCpp
const name = additionalData.name;
const disabled = !isCpp ? false : !(isCpp && answers.isCpp)
return {
name,
value,
disabled
};
});
return [{
name: "Additions",
type: "checkbox",
message: "Select additional sample code",
choices: additionChoices
}]
}
async function generate() {
const templateAnswers = await inquirer.prompt(questions);
const additionQuestions = getAdditions(templateAnswers);
const additionAnswers = await inquirer.prompt(additionQuestions);
if (!fs.existsSync(templateAnswers.iDFPath)) {
console.log(chalk.red("Error: IDF-PATH is invalid"));
//return
}
if (!fs.existsSync(templateAnswers.toolsPath)) {
console.log(chalk.red("Error: ESP tools path is invalid"));
//return
}
let pythonDir = null;
const pythonRootDir = path.join(templateAnswers.toolsPath, "tools", "idf-python");
const pythonDirExists = fs.existsSync(pythonRootDir);
if (pythonDirExists) {
const pythonDistFolderItems = fs.readdirSync(pythonRootDir, { withFileTypes: true })
const pythonDistFolder = pythonDistFolderItems.filter(dir => dir.isDirectory())
if (pythonDistFolder.length > 0) {
pythonDir = path.join(pythonRootDir, pythonDistFolder[0].name, "Scripts")
}
}
if(!pythonDir){
console.log(chalk.yellow("Warning!: Python directory in tools was not discovered"));
}
const answers = {
...templateAnswers,
pythonDir,
...additionAnswers,
}
console.log("answers", answers);
fs.mkdirSync(path.join(CURR_DIR, answers.projectName));
const templatePath = path.join(__dirname, "esp-idf-template");
console.log(chalk.cyan(`Generating Template with name"${answers.projectName}"`));
var templateModel = generateTemplateModel(answers);
createDirectoryContents(templatePath, answers.projectName, templateModel);
//additions
answers.Additions.forEach(addition => {
const outPath = path.resolve(__dirname, "Additions", addition)
const additionsPath = path.resolve(outPath, "files");
if (fs.existsSync(additionsPath)) {
createDirectoryContents(additionsPath, answers.projectName, templateModel);
}
// run additional scripts if present
const additionsScripts = path.resolve(outPath, "index.js");
if (fs.existsSync(additionsScripts)) {
const additionScript = require(additionsScripts);
additionScript.run(path.resolve(CURR_DIR, answers.projectName));
}
});
console.log(chalk.green("Success"));
console.log(chalk.green("see read me for more information or visit us on"));
console.log(chalk.greenBright.bold.underline("https://learnesp32.com"));
console.log(chalk.black.bgRed('Warning! As of vs-code v1.56.1 you must add '));
console.log(chalk.black.bgRed.bold('"terminal.integrated.allowWorkspaceConfiguration":true,'));
console.log(chalk.black.bgRed('to your user settings for the integrated terminal to work. see https://github.com/Mair/create-esp32-app/issues/10'));
console.log(chalk.green("please navigate to your new project and open it in vscode"));
console.log(chalk.cyan(`cd ${answers.projectName}`));
console.log(chalk.cyan("code ."));
}
function createDirectoryContents(templatePath, newProjectPath, templateModel) {
const filesToCreate = fs.readdirSync(templatePath);
filesToCreate.forEach(file => {
const origFilePath = path.resolve(templatePath, file);
const stats = fs.statSync(origFilePath);
if (stats.isFile()) {
const contents = fs.readFileSync(origFilePath, "utf8");
const updatedContents = applyTemplate(contents, templateModel);
if (file === ".npmignore") file = ".gitignore";
if (file.endsWith(".c") && templateModel.isCpp) file += "pp"
const newPath = path.resolve(CURR_DIR, newProjectPath, file);
fs.writeFileSync(newPath, updatedContents, "utf8");
} else if (stats.isDirectory()) {
const newPath = path.resolve(CURR_DIR, newProjectPath, file);
if (!fs.existsSync(newPath)) {
fs.mkdirSync(newPath);
}
createDirectoryContents(path.join(templatePath, file), path.join(newProjectPath, file), templateModel);
}
});
}
function generateTemplateModel(answers) {
const { iDFPath, toolsPath, projectName, pythonDir } = answers;
const forwardSlash_idfPath = iDFPath.replace(/\\/g, "/");
const forwardSlash_toolsPath = toolsPath.replace(/\\/g, "/");
const forwardSlash_elf_Path = `${CURR_DIR.replace(/\\/g, "/")}/${projectName}/build/${projectName}.elf`;
const forwardSlash_python = !pythonDir? null : pythonDir.replace(/\\/g, "/");
const mainModel = getAddition(answers.Additions);
const model = {
IDF_TOOLS_PATH: forwardSlash_toolsPath,
IDF_PATH: forwardSlash_idfPath,
ELF_PATH: forwardSlash_elf_Path,
PROJECT_NAME: projectName,
IDF_PATH_BACKSLASH_ESCAPED: forwardSlash_idfPath,
PYTHON_PATH: forwardSlash_python,
headers: mainModel.headers,
tasks: mainModel.tasks,
functions: mainModel.functions,
globals: mainModel.globals,
extraComponents: mainModel.extraComponents,
isCpp: answers.isCpp
};
return model;
}
function applyTemplate(contents, model) {
const template = UnderscoreTemplate(contents);
const complied = template(model);
return complied;
}
function getAddition(additionalSelections) {
const mainModel = {
headers: [],
globals: [],
tasks: [],
functions: [],
extraComponents: []
};
additionalSelections.forEach(addition => {
const templateJson = require(path.join(__dirname, "Additions", addition, "template.json"));
if (!!templateJson.headers) {
const newHeaders = templateJson.headers.filter(header => !mainModel.headers.includes(header));
mainModel.headers = [...mainModel.headers, ...newHeaders];
}
if (!!templateJson.tasks) {
mainModel.tasks = [...mainModel.tasks, ...templateJson.tasks];
}
if (!!templateJson.function) {
mainModel.functions = [...mainModel.functions, ...templateJson.function];
}
if (!!templateJson.globals) {
mainModel.globals = [...mainModel.globals, ...templateJson.globals];
}
if (!!templateJson.extraComponents) {
mainModel.extraComponents = [...mainModel.extraComponents, ...templateJson.extraComponents];
}
});
return mainModel;
}
generate();