Skip to content

Commit

Permalink
Fslib API (#533)
Browse files Browse the repository at this point in the history
* Refactors the fslib api

* Bumps versions

* Fixes tests

* Fixes unit tests
  • Loading branch information
arcanis authored Oct 16, 2019
1 parent 71803cf commit 35267b5
Show file tree
Hide file tree
Showing 81 changed files with 752 additions and 471 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {NodeFS} from '@yarnpkg/fslib';
import cp from 'child_process';
import {PortablePath, npath} from '@yarnpkg/fslib';
import cp from 'child_process';

interface Options {
cwd: string;
cwd: PortablePath;
env?: Record<string, string>;
}

Expand All @@ -23,7 +23,7 @@ export const execFile = (
return new Promise((resolve, reject) => {
cp.execFile(path, args, {
...options,
cwd: options.cwd ? NodeFS.fromPortablePath(options.cwd) : undefined,
cwd: options.cwd ? npath.fromPortablePath(options.cwd) : undefined,
}, (error, stdout, stderr) => {
stdout = stdout.replace(/\r\n?/g, `\n`);
stderr = stderr.replace(/\r\n?/g, `\n`);
Expand Down
30 changes: 15 additions & 15 deletions packages/acceptance-tests/pkg-tests-core/sources/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {xfs, NodeFS, PortablePath, ppath, Filename} from '@yarnpkg/fslib';
import klaw from 'klaw';
import tarFs from 'tar-fs';
import tmp from 'tmp';
import zlib from 'zlib';
import {Gzip} from 'zlib';
import {Filename, PortablePath, npath, ppath, xfs} from '@yarnpkg/fslib';
import klaw from 'klaw';
import tarFs from 'tar-fs';
import tmp from 'tmp';
import zlib from 'zlib';
import {Gzip} from 'zlib';

import * as miscUtils from './misc';
import * as miscUtils from './misc';

const IS_WIN32 = process.platform === `win32`;

Expand All @@ -16,12 +16,12 @@ export const walk = (
return new Promise((resolve) => {
const paths: PortablePath[] = [];

const walker = klaw(NodeFS.fromPortablePath(source), {
const walker = klaw(npath.fromPortablePath(source), {
filter: (sourcePath: string) => {
if (!filter)
return true;

const itemPath = NodeFS.toPortablePath(sourcePath);
const itemPath = npath.toPortablePath(sourcePath);
const stat = xfs.statSync(itemPath);

if (stat.isDirectory())
Expand All @@ -37,7 +37,7 @@ export const walk = (
});

walker.on('data', ({path: sourcePath}) => {
const itemPath = NodeFS.toPortablePath(sourcePath);
const itemPath = npath.toPortablePath(sourcePath);
const relativePath = ppath.relative(source, itemPath);

if (!filter || miscUtils.filePatternMatch(relativePath, filter))
Expand Down Expand Up @@ -67,9 +67,9 @@ export const packToStream = (

const zipperStream = zlib.createGzip();

const packStream = tarFs.pack(NodeFS.fromPortablePath(source), {
const packStream = tarFs.pack(npath.fromPortablePath(source), {
map: (header: any) => {
header.name = NodeFS.toPortablePath(header.name);
header.name = npath.toPortablePath(header.name);

if (true) {
header.name = ppath.resolve(PortablePath.root, header.name);
Expand Down Expand Up @@ -118,7 +118,7 @@ export const packToFile = (target: PortablePath, source: PortablePath, options:
export const unpackToDirectory = (target: PortablePath, source: PortablePath): Promise<void> => {
const tarballStream = xfs.createReadStream(source);
const gunzipStream = zlib.createUnzip();
const extractStream = tarFs.extract(NodeFS.fromPortablePath(target));
const extractStream = tarFs.extract(npath.fromPortablePath(target));

tarballStream.pipe(gunzipStream).pipe(extractStream);

Expand Down Expand Up @@ -147,7 +147,7 @@ export const createTemporaryFolder = (name?: Filename): Promise<PortablePath> =>
if (error) {
reject(error);
} else {
let realPath = await xfs.realpathPromise(NodeFS.toPortablePath(dirPath));
let realPath = await xfs.realpathPromise(npath.toPortablePath(dirPath));

if (name) {
realPath = ppath.join(realPath, name as PortablePath);
Expand All @@ -173,7 +173,7 @@ export const createTemporaryFile = async (filePath: PortablePath): Promise<Porta
if (error) {
reject(error);
} else {
resolve(NodeFS.toPortablePath(filePath));
resolve(npath.toPortablePath(filePath));
}
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NodeFS} from '@yarnpkg/fslib';
import {npath} from '@yarnpkg/fslib';
import {delimiter} from 'path';

import {URL} from 'url';
Expand All @@ -24,8 +24,8 @@ const mte = generatePkgDriver({
for (const key of Object.keys(config))
rcEnv[`YARN_${key.replace(/([A-Z])/g, `_$1`).toUpperCase()}`] = config[key];

const nativePath = NodeFS.fromPortablePath(path);
const tempHomeFolder = NodeFS.fromPortablePath(await createTemporaryFolder());
const nativePath = npath.fromPortablePath(path);
const tempHomeFolder = npath.fromPortablePath(await createTemporaryFolder());

const cwdArgs = typeof projectFolder !== `undefined`
? [projectFolder]
Expand Down
22 changes: 11 additions & 11 deletions packages/acceptance-tests/pkg-tests-core/sources/utils/tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NodeFS, toFilename} from '@yarnpkg/fslib';
import {PortablePath, npath, toFilename} from '@yarnpkg/fslib';
import crypto from 'crypto';
import {IncomingMessage, ServerResponse} from 'http';
import http from 'http';
Expand All @@ -16,14 +16,14 @@ export type PackageEntry = Map<string, {path: string, packageJson: Object}>;
export type PackageRegistry = Map<string, PackageEntry>;

interface RunDriverOptions extends Record<string, any> {
cwd?: string;
projectFolder?: string;
cwd?: PortablePath;
projectFolder?: PortablePath;
registryUrl: string;
env?: Record<string, string>;
}

export type PackageRunDriver = (
command: string,
path: PortablePath,
args: Array<string>,
opts: RunDriverOptions,
) => Promise<ExecResult>;
Expand All @@ -50,7 +50,7 @@ export const getPackageRegistry = (): Promise<PackageRegistry> => {

return (packageRegistryPromise = (async () => {
const packageRegistry = new Map();
for (const packageFile of await fsUtils.walk(NodeFS.toPortablePath(`${require(`pkg-tests-fixtures`)}/packages`), {
for (const packageFile of await fsUtils.walk(npath.toPortablePath(`${require(`pkg-tests-fixtures`)}/packages`), {
filter: ['package.json'],
})) {
const packageJson = await fsUtils.readJson(packageFile);
Expand Down Expand Up @@ -88,8 +88,8 @@ export const getPackageArchiveStream = async (name: string, version: string): Pr
if (!packageVersionEntry)
throw new Error(`Unknown version "${version}" for package "${name}"`);

return fsUtils.packToStream(NodeFS.toPortablePath(packageVersionEntry.path), {
virtualPath: NodeFS.toPortablePath('/package'),
return fsUtils.packToStream(npath.toPortablePath(packageVersionEntry.path), {
virtualPath: npath.toPortablePath('/package'),
});
};

Expand All @@ -104,8 +104,8 @@ export const getPackageArchivePath = async (name: string, version: string): Prom

const archivePath = await fsUtils.createTemporaryFile(toFilename(`${name}-${version}.tar.gz`));

await fsUtils.packToFile(archivePath, NodeFS.toPortablePath(packageVersionEntry.path), {
virtualPath: NodeFS.toPortablePath('/package'),
await fsUtils.packToFile(archivePath, npath.toPortablePath(packageVersionEntry.path), {
virtualPath: npath.toPortablePath('/package'),
});

return archivePath;
Expand Down Expand Up @@ -266,7 +266,7 @@ export const startPackageServer = (): Promise<string> => {
['Transfer-Encoding']: 'chunked',
});

const packStream = fsUtils.packToStream(NodeFS.toPortablePath(packageVersionEntry.path), {virtualPath: NodeFS.toPortablePath('/package')});
const packStream = fsUtils.packToStream(npath.toPortablePath(packageVersionEntry.path), {virtualPath: npath.toPortablePath('/package')});
packStream.pipe(response);
},

Expand Down Expand Up @@ -504,7 +504,7 @@ export const generatePkgDriver = ({
const registryUrl = await startPackageServer();

// Writes a new package.json file into our temporary directory
await fsUtils.writeJson(NodeFS.toPortablePath(`${path}/package.json`), await deepResolve(packageJson));
await fsUtils.writeJson(npath.toPortablePath(`${path}/package.json`), await deepResolve(packageJson));

const run = (...args: any[]) => {
let callDefinition = {};
Expand Down
4 changes: 2 additions & 2 deletions packages/acceptance-tests/pkg-tests-fixtures/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const {NodeFS} = require('@yarnpkg/fslib');
const {npath} = require('@yarnpkg/fslib');

module.exports = NodeFS.toPortablePath(__dirname);
module.exports = npath.toPortablePath(__dirname);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NodeFS} from '@yarnpkg/fslib';
import {npath} from '@yarnpkg/fslib';

const {
fs: {createTemporaryFolder, readJson, writeJson},
Expand All @@ -19,7 +19,7 @@ describe(`Commands`, () => {

await expect(readJson(`${path}/package.json`)).resolves.toMatchObject({
resolutions: {
[`my-package`]: `portal:${NodeFS.toPortablePath(`${tmp}/my-package`)}`,
[`my-package`]: `portal:${npath.toPortablePath(`${tmp}/my-package`)}`,
},
});
}),
Expand Down Expand Up @@ -49,7 +49,7 @@ describe(`Commands`, () => {

await expect(readJson(`${path}/package.json`)).resolves.toMatchObject({
resolutions: {
[`my-package`]: `portal:${NodeFS.toPortablePath(`${tmp}/my-package`)}`,
[`my-package`]: `portal:${npath.toPortablePath(`${tmp}/my-package`)}`,
},
});
},
Expand Down Expand Up @@ -78,8 +78,8 @@ describe(`Commands`, () => {

await expect(readJson(`${path}/package.json`)).resolves.toMatchObject({
resolutions: {
[`workspace-a`]: `portal:${NodeFS.toPortablePath(`${tmp}/my-workspace/packages/workspace-a`)}`,
[`workspace-b`]: `portal:${NodeFS.toPortablePath(`${tmp}/my-workspace/packages/workspace-b`)}`,
[`workspace-a`]: `portal:${npath.toPortablePath(`${tmp}/my-workspace/packages/workspace-a`)}`,
[`workspace-b`]: `portal:${npath.toPortablePath(`${tmp}/my-workspace/packages/workspace-b`)}`,
},
});
}),
Expand Down Expand Up @@ -108,7 +108,7 @@ describe(`Commands`, () => {

await expect(readJson(`${path}/package.json`)).resolves.toMatchObject({
resolutions: {
[`workspace-a`]: `portal:${NodeFS.toPortablePath(`${tmp}/my-workspace/packages/workspace-a`)}`,
[`workspace-a`]: `portal:${npath.toPortablePath(`${tmp}/my-workspace/packages/workspace-a`)}`,
},
});

Expand Down Expand Up @@ -143,8 +143,8 @@ describe(`Commands`, () => {

await expect(readJson(`${path}/package.json`)).resolves.toMatchObject({
resolutions: {
[`workspace-a`]: `portal:${NodeFS.toPortablePath(`${tmp}/my-workspace/packages/workspace-a`)}`,
[`workspace-b`]: `portal:${NodeFS.toPortablePath(`${tmp}/my-workspace/packages/workspace-b`)}`,
[`workspace-a`]: `portal:${npath.toPortablePath(`${tmp}/my-workspace/packages/workspace-a`)}`,
[`workspace-b`]: `portal:${npath.toPortablePath(`${tmp}/my-workspace/packages/workspace-b`)}`,
},
});
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {NodeFS, xfs} = require(`@yarnpkg/fslib`);
const {npath, xfs} = require(`@yarnpkg/fslib`);
const {
exec: {execFile},
fs: {writeFile, writeJson, mkdirp},
Expand All @@ -14,8 +14,8 @@ describe(`Commands`, () => {

await expect(run(`stage`, `-n`, {cwd: path})).resolves.toMatchObject({
stdout: [
`${NodeFS.fromPortablePath(`${path}/.yarnrc.yml`)}\n`,
`${NodeFS.fromPortablePath(`${path}/package.json`)}\n`,
`${npath.fromPortablePath(`${path}/.yarnrc.yml`)}\n`,
`${npath.fromPortablePath(`${path}/package.json`)}\n`,
].join(``),
});
}),
Expand All @@ -31,8 +31,8 @@ describe(`Commands`, () => {

await expect(run(`stage`, `-n`, {cwd: path})).resolves.toMatchObject({
stdout: [
`${NodeFS.fromPortablePath(`${path}/.yarnrc.yml`)}\n`,
`${NodeFS.fromPortablePath(`${path}/package.json`)}\n`,
`${npath.fromPortablePath(`${path}/.yarnrc.yml`)}\n`,
`${npath.fromPortablePath(`${path}/package.json`)}\n`,
].join(``),
});
}),
Expand All @@ -52,13 +52,13 @@ describe(`Commands`, () => {

await expect(run(`stage`, `-n`, {cwd: path})).resolves.toMatchObject({
stdout: [
`${NodeFS.fromPortablePath(`${path}/.pnp.js`)}\n`,
`${NodeFS.fromPortablePath(`${path}/.yarn/global/cache/no-deps-npm-1.0.0-cf533b267a-1.zip`)}\n`,
`${NodeFS.fromPortablePath(`${path}/.yarn/cache/.gitignore`)}\n`,
`${NodeFS.fromPortablePath(`${path}/.yarn/cache/no-deps-npm-1.0.0-cf533b267a-1.zip`)}\n`,
`${NodeFS.fromPortablePath(`${path}/.yarnrc.yml`)}\n`,
`${NodeFS.fromPortablePath(`${path}/package.json`)}\n`,
`${NodeFS.fromPortablePath(`${path}/yarn.lock`)}\n`,
`${npath.fromPortablePath(`${path}/.pnp.js`)}\n`,
`${npath.fromPortablePath(`${path}/.yarn/global/cache/no-deps-npm-1.0.0-cf533b267a-1.zip`)}\n`,
`${npath.fromPortablePath(`${path}/.yarn/cache/.gitignore`)}\n`,
`${npath.fromPortablePath(`${path}/.yarn/cache/no-deps-npm-1.0.0-cf533b267a-1.zip`)}\n`,
`${npath.fromPortablePath(`${path}/.yarnrc.yml`)}\n`,
`${npath.fromPortablePath(`${path}/package.json`)}\n`,
`${npath.fromPortablePath(`${path}/yarn.lock`)}\n`,
].join(``),
});
}),
Expand Down
14 changes: 7 additions & 7 deletions packages/acceptance-tests/pkg-tests-specs/sources/pnp.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {NodeFS, ppath, xfs} = require(`@yarnpkg/fslib`);
const {npath, ppath, xfs} = require(`@yarnpkg/fslib`);
const cp = require(`child_process`);
const {satisfies} = require(`semver`);

Expand Down Expand Up @@ -452,8 +452,8 @@ describe(`Plug'n'Play`, () => {
await expect(
source(
`require(require.resolve('no-deps', {paths: ${JSON.stringify([
`${NodeFS.fromPortablePath(path)}/workspace-a`,
`${NodeFS.fromPortablePath(path)}/workspace-b`,
`${npath.fromPortablePath(path)}/workspace-a`,
`${npath.fromPortablePath(path)}/workspace-b`,
])}}))`,
),
).resolves.toMatchObject({
Expand Down Expand Up @@ -547,7 +547,7 @@ describe(`Plug'n'Play`, () => {
await writeFile(`${tmp}/index.js`, `require(process.argv[2])`);
await writeFile(`${path}/index.js`, `require('no-deps')`);

await run(`node`, `${NodeFS.fromPortablePath(tmp)}/index.js`, `${path}/index.js`);
await run(`node`, `${npath.fromPortablePath(tmp)}/index.js`, `${path}/index.js`);
},
),
);
Expand Down Expand Up @@ -1092,7 +1092,7 @@ describe(`Plug'n'Play`, () => {
await expect(
source(
`JSON.parse(require('child_process').execFileSync(process.execPath, [${JSON.stringify(
`${NodeFS.fromPortablePath(path)}/script.js`,
`${npath.fromPortablePath(path)}/script.js`,
)}]).toString())`,
),
).resolves.toMatchObject({
Expand All @@ -1117,7 +1117,7 @@ describe(`Plug'n'Play`, () => {
await writeFile(
`${path}/main.js`,
`console.log(require('child_process').execFileSync(process.execPath, [${JSON.stringify(
`${NodeFS.fromPortablePath(path)}/sub.js`,
`${npath.fromPortablePath(path)}/sub.js`,
)}]).toString())`,
);

Expand All @@ -1137,7 +1137,7 @@ describe(`Plug'n'Play`, () => {
await writeFile(`${path}/foo.js`, `console.log(42);`);

await expect(
run(`node`, `-e`, `console.log(21);`, {env: {NODE_OPTIONS: `--require ${NodeFS.fromPortablePath(path)}/foo`}}),
run(`node`, `-e`, `console.log(21);`, {env: {NODE_OPTIONS: `--require ${npath.fromPortablePath(path)}/foo`}}),
).resolves.toMatchObject({
// Note that '42' is present twice: the first one because Node executes Yarn, and the second one because Yarn spawns Node
stdout: `42\n42\n21\n`,
Expand Down
Loading

0 comments on commit 35267b5

Please sign in to comment.