From 94fef9a2b11d96f8b87caf26eb53e328db382504 Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Tue, 5 Mar 2019 14:59:31 -0800 Subject: [PATCH] FEATURE: Add support for relative symlinks in create-symlinks command --- bin/flow-mono | 9 ++++++++- docs/cli/create-symlinks.md | 8 +++++++- .../__snapshots__/create-symlinks.spec.js.snap | 3 +++ src/commands/create-symlinks.js | 6 +++--- src/commands/create-symlinks.spec.js | 2 +- src/lib/dependency.js | 4 ++-- src/lib/file.js | 17 +++++++++++++---- 7 files changed, 37 insertions(+), 12 deletions(-) mode change 100644 => 100755 bin/flow-mono diff --git a/bin/flow-mono b/bin/flow-mono old mode 100644 new mode 100755 index 0c2e3cf2..df683a31 --- a/bin/flow-mono +++ b/bin/flow-mono @@ -30,9 +30,16 @@ const _ = require('yargs') type: 'string', describe: 'The relative path to the `.flowconfig` for which symlinks will be created in all packages with a `flow-bin` dependency' }) + yargs.option('relative', { + alias: 'r', + type: 'boolean', + default: false, + describe: 'If passed the symlinks will be relative paths instead of absolute' + }) }, argv => { asyncUtils.exec(createFlowTypeSymlinks, { - flowConfigPath: argv.path + flowConfigPath: argv.path, + relative: argv.relative }); }) .command('install-types', 'Installs flow-typed typings for all mono-repo package dependencies', () => {}, () => { diff --git a/docs/cli/create-symlinks.md b/docs/cli/create-symlinks.md index 2db20819..afd2dfed 100644 --- a/docs/cli/create-symlinks.md +++ b/docs/cli/create-symlinks.md @@ -16,7 +16,13 @@ $ flow-mono create-symlinks ./build/.flowconfig #### Options and Arguments -The only argument provided should be the relative path to the fallback / singleton `.flowconfig`. +##### `[flowconfig-path]` + +The relative path to the fallback / singleton `.flowconfig`. + +##### `-r, --relative` \(Optional\) + +Create relative symlinks (e.g. ../../../build/.flowconfig) instead of absolute symlinks (e.g. /path/to/build/.flowconfig) #### Configuration diff --git a/src/commands/__snapshots__/create-symlinks.spec.js.snap b/src/commands/__snapshots__/create-symlinks.spec.js.snap index 4e4335c1..0a4e005e 100644 --- a/src/commands/__snapshots__/create-symlinks.spec.js.snap +++ b/src/commands/__snapshots__/create-symlinks.spec.js.snap @@ -5,6 +5,7 @@ Array [ Array [ "/usr/app/foo/.flowconfig", "/foo/baz", + false, ], ] `; @@ -15,11 +16,13 @@ Array [ "baz-dependency", "/foo", "/foo/bar", + false, ], Array [ "baz-dependency", "/foo", "/foo/baz", + false, ], ] `; diff --git a/src/commands/create-symlinks.js b/src/commands/create-symlinks.js index 7d6b5c1d..75eb6303 100644 --- a/src/commands/create-symlinks.js +++ b/src/commands/create-symlinks.js @@ -9,7 +9,7 @@ const file = require('./../lib/file.js'); const {info, success} = require('./../lib/logger.js'); module.exports = async function createFlowTypeSymlinks( - {flowConfigPath}: {flowConfigPath: string}, + {flowConfigPath, relative}: {flowConfigPath: string, relative: boolean}, cwd?: string = process.cwd() ) { const cliConfig = await config.resolveAndReadConfig(); @@ -27,7 +27,7 @@ module.exports = async function createFlowTypeSymlinks( const existsFlowConfig = await file.existsAsync(join(packagePath, '.flowconfig')); if (existsFlowConfig === false) { - await file.createSymlink(absoluteFlowConfigPath, packagePath); + await file.createSymlink(absoluteFlowConfigPath, packagePath, relative); } const packageJson = await dependency.readPackageJson(packagePath); @@ -36,7 +36,7 @@ module.exports = async function createFlowTypeSymlinks( await Promise.all( dependencyKeys.filter(key => ignoredPackageKeys.includes(key) === false).map(key => { - return dependency.createSymlinkForDependency(key, rootPath, packagePath); + return dependency.createSymlinkForDependency(key, rootPath, packagePath, relative); }) ); }) diff --git a/src/commands/create-symlinks.spec.js b/src/commands/create-symlinks.spec.js index 0cbe200e..086839b1 100644 --- a/src/commands/create-symlinks.spec.js +++ b/src/commands/create-symlinks.spec.js @@ -34,7 +34,7 @@ describe('create-symlinks', () => { dependency.readPackageJson.mockReturnValue({}); dependency.mergeDependenciesIntoList.mockReturnValue(['foo-dependency', 'bar-dependency', 'baz-dependency']); - await createFlowTypeSymlinks({flowConfigPath: '/foo/.flowconfig'}, '/usr/app'); + await createFlowTypeSymlinks({flowConfigPath: '/foo/.flowconfig', relative: false}, '/usr/app'); expect(file.createSymlink.mock.calls).toMatchSnapshot(); expect(dependency.createSymlinkForDependency.mock.calls).toMatchSnapshot(); diff --git a/src/lib/dependency.js b/src/lib/dependency.js index ebe5d349..0085e335 100644 --- a/src/lib/dependency.js +++ b/src/lib/dependency.js @@ -166,7 +166,7 @@ const dependencyUtils = { * @param {String} packageDir The package directory in whichs `node_modules` folder we should create the symlink. * @return {Promise} A Promise that resolves once the symlink was created. */ - async createSymlinkForDependency(key: string, rootDir: string, packageDir: string) { + async createSymlinkForDependency(key: string, rootDir: string, packageDir: string, relative: booelan) { this.ensureDependencyScopeExists(key, packageDir); const scope = this.getScopeForDependency(key); @@ -187,7 +187,7 @@ const dependencyUtils = { return; } - await file.createSymlink(src, distDir); + await file.createSymlink(src, distDir, relative); } }; diff --git a/src/lib/file.js b/src/lib/file.js index 00403dbd..6758c178 100644 --- a/src/lib/file.js +++ b/src/lib/file.js @@ -1,7 +1,8 @@ // @flow const fs = require('fs'); -const {basename, join} = require('path'); +const process = require('process'); +const path = require('path'); const {promisify} = require('util'); const {error} = require('./logger.js'); @@ -16,13 +17,21 @@ const _utils = { const fileUtils = { _utils, - async createSymlink(target: string, distDir: string) { - const dist = join(distDir, basename(target)); + async createSymlink(target: string, distDir: string, relative: boolean) { + const dist = path.join(distDir, path.basename(target)); const stats = await _utils.statAsync(target); // Use a junction on Windows like Yarn do. // See: https://github.com/yarnpkg/yarn/blob/fc94a16b7ca90a188d084aef8cea406b60e8c38f/src/util/fs.js#L695-L696 const type = stats.isDirectory() ? 'junction' : 'file'; - await _utils.symlinkAsync(target, dist, type); + let targetRelative = target; + if (relative) { + targetRelative = path.relative(path.dirname(dist), target); + } + + const currDur = process.cwd(); + process.chdir(distDir); + await _utils.symlinkAsync(targetRelative, path.basename(target), type); + process.chdir(currDur); }, /**