Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #220 from zrsmith92/zsmith/relative-symlinks
Browse files Browse the repository at this point in the history
Add support for relative symlinks in create-symlinks command
  • Loading branch information
Inkdpixels authored Mar 21, 2019
2 parents 6356bcb + 94fef9a commit 9b8068a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 12 deletions.
9 changes: 8 additions & 1 deletion bin/flow-mono
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {}, () => {
Expand Down
8 changes: 7 additions & 1 deletion docs/cli/create-symlinks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/commands/__snapshots__/create-symlinks.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Array [
Array [
"/usr/app/foo/.flowconfig",
"/foo/baz",
false,
],
]
`;
Expand All @@ -15,11 +16,13 @@ Array [
"baz-dependency",
"/foo",
"/foo/bar",
false,
],
Array [
"baz-dependency",
"/foo",
"/foo/baz",
false,
],
]
`;
6 changes: 3 additions & 3 deletions src/commands/create-symlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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);
})
);
})
Expand Down
2 changes: 1 addition & 1 deletion src/commands/create-symlinks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -187,7 +187,7 @@ const dependencyUtils = {
return;
}

await file.createSymlink(src, distDir);
await file.createSymlink(src, distDir, relative);
}
};

Expand Down
17 changes: 13 additions & 4 deletions src/lib/file.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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);
},

/**
Expand Down

0 comments on commit 9b8068a

Please sign in to comment.