@inlang/paraglide-js
Version:
[](https://www.npmjs.com/package/@inlang/paraglide-js) [
.name("init")
.summary("Initializes Paraglide-JS.")
.action(async () => {
const logger = new Logger({ silent: false, prefix: false });
logger.box("Welcome to Paraglide JS 🪂");
const ctx = {
logger,
fs: nodeFs.promises,
syncFs: nodeFs,
root: process.cwd(),
};
const ctx1 = await checkForUncommittedChanges(ctx);
const ctx2 = await enforcePackageJsonExists(ctx1);
const ctx3 = await initializeInlangProject(ctx2);
const ctx4 = await promptForOutdir(ctx3);
const ctx5 = await addParaglideJsToDevDependencies(ctx4);
const ctx6 = await detectBundler(ctx5);
if (ctx6.bundler === "vite") {
await addVitePlugin(ctx6);
}
else {
await addCompileStepToPackageJSON(ctx6);
}
const ctx7 = await maybeUpdateTsConfig(ctx6);
const ctx8 = await maybeAddSherlock(ctx7);
const ctx9 = await maybeAddMachineTranslation(ctx8);
try {
await compile({
project: ctx9.projectPath,
outdir: ctx9.outdir,
});
ctx.logger.success("Ran the paraglide compiler");
}
catch {
ctx.logger.warn("Failed to compile project automatically. You will need to run the compiler manually");
}
const successMessage = [
"Setup complete! Run `npm install` and then `npm run build`.",
`Docs: https://paraglidejs.com/basics`,
"\n",
"For questions and feedback, visit",
"https://github.com/opral/paraglide-js/issues",
].join("\n");
ctx.logger.box(successMessage);
process.exit(0);
});
const addParaglideJsToDevDependencies = async (ctx) => {
const ctx1 = await updatePackageJson({
devDependencies: async (devDeps) => ({
...devDeps,
"@inlang/paraglide-js": `^${ENV_VARIABLES.PARJS_PACKAGE_VERSION}`,
}),
})(ctx);
ctx.logger.success("Added @inlang/paraglide-js to the devDependencies in package.json.");
return ctx1;
};
const enforcePackageJsonExists = async (ctx) => {
const packageJsonPath = await findPackageJson(ctx.fs, process.cwd());
if (!packageJsonPath) {
ctx.logger.warn("No package.json found in the current working directory. Please change the working directory to the directory with a package.json file.");
return process.exit(0);
}
return { ...ctx, packageJsonPath };
};
const addCompileStepToPackageJSON = async (ctx) => {
const projectPath = "./" + nodePath.relative(process.cwd(), ctx.projectPath);
const outdir = "./" + nodePath.relative(process.cwd(), ctx.outdir);
ctx = await updatePackageJson({
scripts: async (scripts) => {
if (scripts.build === undefined) {
scripts.build = `paraglide-js compile --project ${projectPath} --outdir ${outdir}`;
}
else if (scripts.build.includes("paraglide-js compile") === false) {
scripts.build = `paraglide-js compile --project ${projectPath} --outdir ${outdir} && ${scripts.build}`;
}
else {
return scripts;
}
ctx.logger.success("Added the compile command to the build step in package.json.");
ctx.logger.info(`Visit https://paraglidejs.com/compiling-messages for more information.`);
return scripts;
},
})(ctx);
return ctx;
};
//# sourceMappingURL=command.js.map