forked from Mair/create-esp32-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
231 lines (209 loc) · 7.63 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
#!/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");
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);
const answers = {
...templateAnswers,
...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.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 } = 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 backSlash_idf_path_escaped = forwardSlash_idfPath.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: backSlash_idf_path_escaped,
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();