ardunno-sketch
Version:
Utility for Arduino sketches
111 lines (110 loc) • 5.23 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sketchbookTreeSync = exports.sketchbookTree = exports.filterNestedMainSketchFiles = exports.findMainSketchFilesSync = exports.findMainSketchFiles = exports.isMainSketchFile = void 0;
const globby_1 = __importStar(require("globby"));
const path_1 = require("path");
const set_value_1 = __importDefault(require("set-value"));
/**
* Tests if the `path` is a main sketch file based on the [specs](https://arduino.github.io/arduino-cli/0.27/sketch-specification/#sketch-folders-and-files).
*
* It does **not** check whether the resource is accessible.
*/
function isMainSketchFile(path) {
return /([0-9a-zA-Z]{1}[0-9a-zA-Z_\.-]{0,62})(\/|\\)\1.(ino|pde)$/.test(path);
}
exports.isMainSketchFile = isMainSketchFile;
/**
* Returns a promise which resolves with an array of all valid main sketch filenames recursively discovered from the `cwd` directory.
* By default, filters the sketches from the `hardware` and `libraries` folders.
* The array might contain nested main sketch files. For example: `['/path/to/Sketch/Sketch.ino', '/path/to/Sketch/Nested/Nested.ino']`.
* @param cwd the root path to start the sketch discovery from.
*/
async function findMainSketchFiles(cwd) {
const paths = await (0, globby_1.default)(mainSketchFilePattern, { cwd });
return paths.map((path) => (0, path_1.join)(cwd, path)).filter(isMainSketchFile);
}
exports.findMainSketchFiles = findMainSketchFiles;
function findMainSketchFilesSync(cwd) {
const paths = (0, globby_1.sync)(mainSketchFilePattern, { cwd });
return paths.map((path) => (0, path_1.join)(cwd, path)).filter(isMainSketchFile);
}
exports.findMainSketchFilesSync = findMainSketchFilesSync;
const mainSketchFilePattern = ['**/*.{ino,pde}', '!hardware/**', '!libraries/**'];
/**
* Filters `['/path/to/Sketch/Sketch.ino', '/path/to/Sketch/Nested/Nested.ino']` to `['/path/to/Sketch/Sketch.ino']`.
*/
function filterNestedMainSketchFiles(mainSketchFiles) {
return mainSketchFiles
.slice()
.sort((left, right) => left.length - right.length)
.reduce((acc, mainSketchFile) => {
if (!acc.map(path_1.dirname).some((otherMainSketchFile) => mainSketchFile.startsWith(otherMainSketchFile))) {
acc.push(mainSketchFile);
}
return acc;
}, []);
}
exports.filterNestedMainSketchFiles = filterNestedMainSketchFiles;
/**
* Builds a sketchbook tree by discovering main sketch files from the root `cwd`.
* The root arg can be a sketch folder.
*/
async function sketchbookTree(cwd) {
const allMainSketchFiles = await findMainSketchFiles(cwd);
return build(cwd, allMainSketchFiles);
}
exports.sketchbookTree = sketchbookTree;
function sketchbookTreeSync(cwd) {
const allMainSketchFiles = findMainSketchFilesSync(cwd);
return build(cwd, allMainSketchFiles);
}
exports.sketchbookTreeSync = sketchbookTreeSync;
function build(cwd, allMainSketchFiles) {
if (allMainSketchFiles.length === 1 && (0, path_1.dirname)(allMainSketchFiles[0]) === cwd) {
return {
cwd,
root: (0, path_1.basename)(allMainSketchFiles[0]),
};
}
const mainSketchFiles = filterNestedMainSketchFiles(allMainSketchFiles);
const root = mainSketchFiles.reduce((sketchbook, mainSketchFile) => {
const relativePath = (0, path_1.relative)(cwd, mainSketchFile);
const segments = relativePath.split(path_1.sep);
if (segments.length < 2) {
throw new Error(`Expected at least two segments relative path from the root. cwd: ${cwd}, mainSketchFile: ${mainSketchFile}, relative path: ${relativePath}`);
}
const name = segments.pop(); // pop the filename segment
if (!name) {
throw new Error(`Empty filename from relative path ${relativePath}. cwd: ${cwd}, mainSketchFile: ${mainSketchFile}, relative path: ${relativePath}`);
}
(0, set_value_1.default)(sketchbook, segments, name);
return sketchbook;
}, {});
return { cwd, root };
}