Skip to content

Commit

Permalink
added --targetExt or -tx
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcelh1983 committed Oct 11, 2024
1 parent 2ba657e commit 910bef7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 49 deletions.
3 changes: 2 additions & 1 deletion lib/tspci/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ Available Commands
--dev -d Start development server
--watch -w Only watch changes
--help -h Help about commandos
--target -t Build production for platform, could be @citolab/tscpi-${target} or a fully qualified specified npm package name.
--target -t Build production for platform, could be @citolab/tscpi-${target}
--targetExt -tx Same as -target but reffering to a fully qualified package (not in @citolab)
init Init PCI development environment.
add --target Add specific implementation to the PCI.
Expand Down
76 changes: 28 additions & 48 deletions lib/tspci/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const watchBuild = (devServer = false, target = "IMS") => {
if (!fs.existsSync(PACKAGE_DIST_PATH)) {
fs.mkdirSync(PACKAGE_DIST_PATH);
}

if (devServer) {
try {
if (fs.existsSync(path.join(PACKAGE_ROOT_PATH, "src/index.html"))) {
Expand All @@ -52,13 +51,6 @@ const watchBuild = (devServer = false, target = "IMS") => {
fileToCopy = path.join(__dirname, `../public/index.html`);
} else {
fileToCopy = path.join(NODE_MODULES, `@citolab/tspci-${target}/public/index.html`);
if (!fs.existsSync(fileToCopy)) {
fileToCopy = path.join(NODE_MODULES, `${target}/public/index.html`);
}
if (!fs.existsSync(fileToCopy)) {
console.error(`No index.html found for ${target}`);
process.exit();
}
}
fs.copyFile(fileToCopy, path.join(PACKAGE_DIST_PATH, "index.html"), (err) => {
if (err) throw err;
Expand Down Expand Up @@ -193,8 +185,10 @@ const run = async () => {
const help = args.indexOf("--help") >= 0 || args.indexOf("-h") >= 0;
const watch = args.indexOf("--watch") >= 0 || args.indexOf("-w") >= 0;
const dev = args.indexOf("--dev") >= 0 || args.indexOf("-d") >= 0;
const target = args.indexOf("--target") >= 0 || args.indexOf("-t") >= 0;
const t = args.indexOf("--target") >= 0 || args.indexOf("-t") >= 0;
const tExt = args.indexOf("--targetExt") >= 0 || args.indexOf("-tx") >= 0;
const init = args.indexOf("init") >= 0;
const targetName = t !== -1 ? `@citolab/tspci-${t}` : tExt !== -1 ? tExt : null;

if (help) {
console.log(`•\t${chalk.blueBright("--help -h")} list these commands`);
Expand Down Expand Up @@ -353,6 +347,9 @@ const run = async () => {
pciName
);
// point to the javascript file
const packageJson = path.join(destinationFolder, "package.json");
const packageJsonContent = fs.readFileSync(packageJson, "utf8");
let packageJsonContentJson = JSON.parse(packageJsonContent);
packageJsonContentJson.source = "src/index.js";
const newPackageJsonContent = JSON.stringify(packageJsonContentJson, null, 2);
fs.writeFileSync(packageJson, newPackageJsonContent);
Expand Down Expand Up @@ -380,40 +377,31 @@ const run = async () => {
return;
}
const version = args.indexOf("--next") !== -1 ? "next" : "latest";
const targetname = args[targetIndex + 1];
const packageFileContent = fs.readFileSync(path.join(PACKAGE_ROOT_PATH, "package.json"), "utf8");
const packageJson = JSON.parse(packageFileContent);

const modifyPci = () => {
let target = `@citolab/tspci-${targetname}`;
let scriptFile = path.join(path.join(path.join(NODE_MODULES, target), "src"), `init.mjs`);
if (!fs.existsSync(scriptFile)) {
target = targetname;
scriptFile = path.join(path.join(path.join(NODE_MODULES, target), "src"), `init.mjs`);
}
if (!fs.existsSync(scriptFile)) {
console.error(`No init script found for ${targetname}`);
return;
}
const scriptFile = path.join(path.join(path.join(NODE_MODULES, `${targetName}`), "src"), `init.mjs`);
let init = false;
// run init() script
import(`file:///${scriptFile}`).then((script) => {
if (script.init) {
init = true;
console.log(chalk.green(`Running init script for ${target}`));
script.init();
} else {
if (!init) {
console.log(chalk.red(`No init script found for ${targetname}`));
if (fs.existsSync(scriptFile)) {
// run init() script
import(`file:///${scriptFile}`).then((script) => {
if (script.init) {
init = true;
console.log(chalk.green(`Running init script for ${targetName}`));
script.init();
} else {
if (!init) {
console.log(chalk.red(`No init script found for ${targetName}`));
}
}
}
});
});
}
};

// check if target is already in devDependencies
if (!packageJson.devDependencies || !packageJson.devDependencies[target]) {
console.log(chalk.white(`Adding target: ${target}`));
execSync(`npm install ${target}@${version} --save-dev`, {
if (!packageJson.devDependencies || !packageJson.devDependencies[targetName]) {
console.log(chalk.white(`Adding ${targetName}`));
// install @citolab/tspci-${targetname} to devDepencies
execSync(`npm install ${targetName}@${version} --save-dev`, {
cwd: PACKAGE_ROOT_PATH,
stdio: [0, 1, 2],
});
Expand Down Expand Up @@ -441,19 +429,11 @@ const run = async () => {
}

prodBuild().then(() => {
if (target) {
const targetIndex = args.indexOf("--target");
if (args.length <= targetIndex + 1) {
console.error("Missing target name. Use tspci --target target");
}
const targetname = args[targetIndex + 1];
let scriptFile = path.join(
path.join(path.join(path.join(NODE_MODULES, `@citolab`), `tspci-${targetname}`), "src"),
`createPackage.mjs`
);
if (!fs.existsSync(scriptFile)) {
scriptFile = path.join(path.join(path.join(NODE_MODULES, targetname), "src"), `createPackage.mjs`);
if (t) {
if (!targetName) {
console.error("Missing target name. Use tspci --target target or tspci --targetExt target");
}
const scriptFile = path.join(path.join(path.join(NODE_MODULES, `${targetName}`), "src"), `createPackage.mjs`);
import(`file:///${scriptFile}`).then((script) => {
script.bundle();
});
Expand Down

0 comments on commit 910bef7

Please sign in to comment.