@iobroker/create-adapter
Version:
Command line utility to create customized ioBroker adapters
160 lines • 6.21 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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__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.createFiles = createFiles;
exports.writeFiles = writeFiles;
exports.readFile = readFile;
exports.readFileFromRootDir = readFileFromRootDir;
const translate_1 = require("@iobroker/adapter-dev/build/translate");
const fs = __importStar(require("fs-extra"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const templates_1 = __importDefault(require("../../templates"));
const tools_1 = require("./tools");
/**
*
* @param answers
*/
async function createFiles(answers) {
(0, translate_1.resetRateLimitState)();
const creatorVersion = (0, tools_1.getOwnVersion)();
const answersWithMeta = {
...answers,
creatorVersion,
};
const files = await Promise.all(templates_1.default.map(async ({ name, templateFunction }) => {
const customPath = typeof templateFunction.customPath === "function"
? templateFunction.customPath(answersWithMeta)
: typeof templateFunction.customPath === "string"
? templateFunction.customPath
: name.replace(/\.ts$/i, "");
const templateResult = templateFunction(answersWithMeta);
return {
name: customPath,
content: templateResult instanceof Promise ? await templateResult : templateResult,
noReformat: templateFunction.noReformat === true,
};
}));
const necessaryFiles = files.filter(f => f.content != undefined);
return formatFiles(answers, necessaryFiles);
}
/**
* Formats files that are not explicitly forbidden to be formatted
*
* @param answers
* @param files
*/
function formatFiles(answers, files) {
// Normalize indentation considering user preference
const indentation = answers.indentation === "Tab" ? tools_1.indentWithTabs : tools_1.indentWithSpaces;
// Remove multiple subsequent empty lines (can happen during template creation).
const removeEmptyLines = (text) => {
return (text &&
text
.replace(/\r\n/g, "\n")
.replace(/^(\s*\n){2,}/gm, "\n")
.replace(/\n/g, os.EOL));
};
const trimWhitespaceLines = (text) => text && text.replace(/^[ \t]+$/gm, "");
const formatter = (text) => trimWhitespaceLines(removeEmptyLines(indentation(text)));
const usePrettier = answers.tools && answers.tools.indexOf("Prettier") > -1;
const formatted = files.map(async (f) => {
if (f.noReformat || typeof f.content !== "string") {
return f;
}
if (usePrettier && /\.(jsx?|json|tsx?)$/.test(f.name)) {
// Use prettier to format JS/TS/JSON code
const extension = f.name.slice(f.name.lastIndexOf(".") + 1);
f.content = await (0, tools_1.formatWithPrettier)(f.content, answers, extension);
}
else {
// We are using our own handmade formatters
// 1st step: Apply formatters that are valid for all files
f.content = formatter(f.content);
// 2nd step: Apply more specialized formatters
if (answers.quotes != undefined) {
if (f.name.endsWith(".js") || f.name.endsWith(".jsx")) {
f.content = (0, tools_1.jsFixQuotes)(f.content, answers.quotes);
}
else if (f.name.endsWith(".ts") || f.name.endsWith(".tsx")) {
f.content = (0, tools_1.tsFixQuotes)(f.content, answers.quotes);
}
}
}
return f;
});
return Promise.all(formatted);
}
/**
*
* @param targetDir
* @param files
*/
async function writeFiles(targetDir, files) {
// write the files and make sure the target dirs exist
for (const file of files) {
await fs.outputFile(path.join(targetDir, file.name), file.content, typeof file.content === "string" ? "utf8" : undefined);
}
}
/**
*
* @param file
* @param relativeTo
* @param binary
*/
async function readFile(file, relativeTo, binary = false) {
const absolutePath = path.join(relativeTo, file);
if (binary) {
return fs.readFile(absolutePath);
}
return fs.readFile(absolutePath, "utf8");
}
/**
* Reads a file that resides on the root dir. After compilation, this is one folder higher than at build time
*
* @param file
* @param relativeTo
* @param binary
*/
async function readFileFromRootDir(file, relativeTo, binary = false) {
if (await fs.pathExists(path.join(relativeTo, file))) {
return readFile(file, relativeTo, binary);
}
return readFile(path.join("..", file), relativeTo, binary);
}
//# sourceMappingURL=createAdapter.js.map