|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// [start-readme] |
| 4 | +// |
| 5 | +// This script is run on a writer's machine while developing Early Access content locally. |
| 6 | +// You must pass the script the location of your local copy of |
| 7 | +// the `github/docs-early-access` git repo as the first argument. |
| 8 | +// |
| 9 | +// [end-readme] |
| 10 | + |
| 11 | +const { execSync } = require('child_process') |
| 12 | +const rimraf = require('rimraf').sync |
| 13 | +const fs = require('fs') |
| 14 | +const path = require('path') |
| 15 | + |
| 16 | +// Early Access details |
| 17 | +const earlyAccessDirName = 'early-access' |
| 18 | + |
| 19 | +if (!process.argv[2]) { |
| 20 | + throw new Error('Must provide the location of your local `docs-early-access` repo directory') |
| 21 | +} |
| 22 | + |
| 23 | +const earlyAccessLocalRepoDir = path.resolve(process.cwd(), process.argv[2]) |
| 24 | + |
| 25 | +let dirStats |
| 26 | +try { |
| 27 | + dirStats = fs.statSync(earlyAccessLocalRepoDir) |
| 28 | +} catch (err) { |
| 29 | + dirStats = null |
| 30 | +} |
| 31 | + |
| 32 | +if (!dirStats) { |
| 33 | + throw new Error('The local `docs-early-access` repo directory does not exist:', earlyAccessLocalRepoDir) |
| 34 | +} |
| 35 | +if (dirStats && !dirStats.isDirectory()) { |
| 36 | + throw new Error('A non-directory entry exists at the local `docs-early-access` repo directory location:', earlyAccessLocalRepoDir) |
| 37 | +} |
| 38 | + |
| 39 | +const destinationDirNames = ['content', 'data', 'assets/images'] |
| 40 | +const destinationDirsMap = destinationDirNames |
| 41 | + .reduce( |
| 42 | + (map, dirName) => { |
| 43 | + map[dirName] = path.join(process.cwd(), dirName, earlyAccessDirName) |
| 44 | + return map |
| 45 | + }, |
| 46 | + {} |
| 47 | + ) |
| 48 | + |
| 49 | +// Remove all existing early access directories from this repo |
| 50 | +destinationDirNames.forEach(key => rimraf(destinationDirsMap[key])) |
| 51 | + |
| 52 | +// Move the latest early access source directories into this repo |
| 53 | +destinationDirNames.forEach((dirName) => { |
| 54 | + const sourceDir = path.join(earlyAccessLocalRepoDir, dirName) |
| 55 | + const destDir = destinationDirsMap[dirName] |
| 56 | + |
| 57 | + // If the source directory doesn't exist, skip it |
| 58 | + if (!fs.existsSync(sourceDir)) { |
| 59 | + console.warn(`Early access directory '${dirName}' does not exist. Skipping...`) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + // Create a symbolic link to the directory |
| 64 | + fs.symlinkSync(sourceDir, destDir, 'junction') |
| 65 | + |
| 66 | + // Confirm the newly moved directory exist |
| 67 | + if (!fs.existsSync(destDir)) { |
| 68 | + throw new Error(`Failed to symlink early access directory '${dirName}'!`) |
| 69 | + } |
| 70 | + if (!fs.lstatSync(destDir).isSymbolicLink()) { |
| 71 | + throw new Error(`The early access directory '${dirName}' entry is not a symbolic link!`) |
| 72 | + } |
| 73 | + if (!fs.statSync(destDir).isDirectory()) { |
| 74 | + throw new Error(`The early access directory '${dirName}' entry's symbolic link does not refer to a directory!`) |
| 75 | + } |
| 76 | + |
| 77 | + console.log(`Successfully symlinked early access directory '${dirName}' into this repo`) |
| 78 | +}) |
0 commit comments