From fc917569b4028a86c44fdcbc7eb1491a8267b99d Mon Sep 17 00:00:00 2001 From: "James M. Greene" Date: Wed, 11 Nov 2020 00:58:16 -0600 Subject: [PATCH] Add script to symlink to local early-access repo --- .gitignore | 6 +- .../early-access/symlink-from-local-repo.js | 78 +++++++++++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 script/early-access/symlink-from-local-repo.js diff --git a/.gitignore b/.gitignore index a561426c0849..b8d20835cce5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,9 @@ /node_modules/ npm-debug.log coverage/ -/assets/images/early-access/ -/content/early-access/ -/data/early-access/ +/assets/images/early-access +/content/early-access +/data/early-access # blc: broken link checker blc_output.log diff --git a/script/early-access/symlink-from-local-repo.js b/script/early-access/symlink-from-local-repo.js new file mode 100644 index 000000000000..145ea46c34e8 --- /dev/null +++ b/script/early-access/symlink-from-local-repo.js @@ -0,0 +1,78 @@ +#!/usr/bin/env node + +// [start-readme] +// +// This script is run on a writer's machine while developing Early Access content locally. +// You must pass the script the location of your local copy of +// the `github/docs-early-access` git repo as the first argument. +// +// [end-readme] + +const { execSync } = require('child_process') +const rimraf = require('rimraf').sync +const fs = require('fs') +const path = require('path') + +// Early Access details +const earlyAccessDirName = 'early-access' + +if (!process.argv[2]) { + throw new Error('Must provide the location of your local `docs-early-access` repo directory') +} + +const earlyAccessLocalRepoDir = path.resolve(process.cwd(), process.argv[2]) + +let dirStats +try { + dirStats = fs.statSync(earlyAccessLocalRepoDir) +} catch (err) { + dirStats = null +} + +if (!dirStats) { + throw new Error('The local `docs-early-access` repo directory does not exist:', earlyAccessLocalRepoDir) +} +if (dirStats && !dirStats.isDirectory()) { + throw new Error('A non-directory entry exists at the local `docs-early-access` repo directory location:', earlyAccessLocalRepoDir) +} + +const destinationDirNames = ['content', 'data', 'assets/images'] +const destinationDirsMap = destinationDirNames + .reduce( + (map, dirName) => { + map[dirName] = path.join(process.cwd(), dirName, earlyAccessDirName) + return map + }, + {} + ) + +// Remove all existing early access directories from this repo +destinationDirNames.forEach(key => rimraf(destinationDirsMap[key])) + +// Move the latest early access source directories into this repo +destinationDirNames.forEach((dirName) => { + const sourceDir = path.join(earlyAccessLocalRepoDir, dirName) + const destDir = destinationDirsMap[dirName] + + // If the source directory doesn't exist, skip it + if (!fs.existsSync(sourceDir)) { + console.warn(`Early access directory '${dirName}' does not exist. Skipping...`) + return + } + + // Create a symbolic link to the directory + fs.symlinkSync(sourceDir, destDir, 'junction') + + // Confirm the newly moved directory exist + if (!fs.existsSync(destDir)) { + throw new Error(`Failed to symlink early access directory '${dirName}'!`) + } + if (!fs.lstatSync(destDir).isSymbolicLink()) { + throw new Error(`The early access directory '${dirName}' entry is not a symbolic link!`) + } + if (!fs.statSync(destDir).isDirectory()) { + throw new Error(`The early access directory '${dirName}' entry's symbolic link does not refer to a directory!`) + } + + console.log(`Successfully symlinked early access directory '${dirName}' into this repo`) +})