Skip to content

Commit

Permalink
[templates] Add script to restore yarn.lock (#1004)
Browse files Browse the repository at this point in the history
* added script to restore yarn.lock

* added jss doc comments

* removed the script and added it as a function

* added developing templates flag

* updated devTempaltes flag

* updated comment

* changes name of restore file config
  • Loading branch information
addy-pathania authored May 6, 2022
1 parent 56d8e82 commit a4a583c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/create-sitecore-jss/scripts/watch-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,53 @@ import chalk from 'chalk';
import chokidar from 'chokidar';
import path from 'path';
import { initRunner } from '../src/init-runner';
const execSync = require('child_process').execSync;

chokidar
.watch(path.join(process.cwd(), '.\\src\\templates'), { ignoreInitial: true })
.on('ready', () => ready())
.on('all', (event, path) => callback(event, path));

/**
* Initializes the apps.
*/
async function ready() {
console.log(chalk.green('Initializing app...'));
await initializeApps(false);
console.log(chalk.green('Initializing app complete. Watching for changes...'));
}

/**
* Callback for chokidar.
* @param {string} event - The event that occurred.
* @param {string} path - The path of the file that was changed.
*/
async function callback(event?: string, path?: string) {
const color = event === 'add' ? chalk.green : event === 'unlink' ? chalk.red : chalk.white;
console.table(color(`${event} ${path}`));
await initializeApps(true);
}

/**
* restore dependencies added to yarn.lock file post initializing a sample app using the watch script.
* this is necessary so that these dependencies dont get committed to the source control.
*/
function restoreLockfile() {
const output = execSync('git status', { encoding: 'utf-8' });
if (output.includes('yarn.lock')) {
execSync('git restore ../../yarn.lock', { encoding: 'utf-8' });
}
}

const initializeApps = async (noInstall: boolean) => {
let watch;
try {
watch = await import(path.resolve('watch.json'));
const initializers = watch.initializers || [];
await initRunner(initializers, { ...watch.args, templates: initializers, noInstall });
if (watch.args.restoreLockfile) {
restoreLockfile();
}
} catch (error) {
console.log(chalk.red('An error occurred: ', error));
if (!watch) {
Expand Down
5 changes: 4 additions & 1 deletion packages/create-sitecore-jss/watch.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"language": "da-DK",
"fetchWith": "GraphQL",
"prerender": "SSG",
"hostName": "sitecore-jss-nextjs.dev.local"
"hostName": "sitecore-jss-nextjs.dev.local",
// if you're developing packages i.e installing dependencies in packages before initializing an app.
// Set 'restoreLockfile' to false or else your yarn.lock will get restored.
"restoreLockfile": true
}
}

0 comments on commit a4a583c

Please sign in to comment.