Skip to content

Commit

Permalink
Merge pull request #153 from ianwremmel/issue/152-gen-local-paths
Browse files Browse the repository at this point in the history
fix local deps
  • Loading branch information
ianwremmel committed May 2, 2018
2 parents 7961a48 + 94d728b commit a404a1f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/lib/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ export function colorize(value: any): string {
case 'number':
return chalk.yellow(value);
case 'string':
// node modules
if (value.startsWith('@')) {
return chalk.cyan(value);
}
// filenames
if (value.includes('/')) {
return chalk.green(value);
return chalk.blue(value);
}
return chalk.blue(value);
// strings
return chalk.yellow(value);
default:
return chalk.grey(value);
}
Expand Down
28 changes: 24 additions & 4 deletions src/lib/deps.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {existsSync, readFileSync} from 'fs';
import {dirname, resolve} from 'path';
import {dirname, join, resolve} from 'path';

import {format as f, makeDebug} from './debug';
import detective from './detective';
import {findEntryPoints, read, write} from './packages';
import {findEntryPoints, getPackagePath, read, write} from './packages';
import {read as readProject} from './project';

const debug = makeDebug(__filename);
Expand All @@ -20,14 +20,34 @@ interface VersionedDependencies {
* Adds version strings from the root package.json to the passed in set of
* dependencies.s
* @param deps
* @param packageName - needed so we can determine how to update local file:
* paths
*/
async function addVersionsToDeps(
deps: string[],
packageName: string,
): Promise<VersionedDependencies> {
const proj = await readProject();
const descender = join(
...Array((await getPackagePath(packageName)).split('/').length).fill('..'),
);

debug(f`Adding versions for ${packageName} dependencies`);
return deps.reduce(
(acc, dep) => {
acc[dep] = proj.dependencies[dep];
let version = proj.dependencies[dep];
debug(f`found ${version} for ${dep}`);
if (version.startsWith('file:')) {
debug(
f`${version} is a local path. making it relative to ${packageName}`,
);
const relPath = version.replace(/^file:\/*/, '');
const newVersion = `file:${join(descender, relPath)}`;
debug(f`replacing ${version} with ${newVersion}`);
version = newVersion;
}

acc[dep] = version;
return acc;
},
{} as VersionedDependencies,
Expand Down Expand Up @@ -109,7 +129,7 @@ function findRequires(filePath: string): string[] {
export async function generate(packageName: string) {
const deps = await list(packageName);

const versionedDeps = await addVersionsToDeps(deps);
const versionedDeps = await addVersionsToDeps(deps, packageName);

const pkg = await read(packageName);
pkg.dependencies = versionedDeps;
Expand Down

0 comments on commit a404a1f

Please sign in to comment.