Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function main() {
// Read the yarn.lock file and parse it.
//
let file = fs.readFileSync('yarn.lock', 'utf8');
let yarn = lockfile.parse(file);
if (yarn.type !== 'success') {
throw new Error('Lockfile parse failed: ' + JSON.stringify(yarn, null, 2));
}
// Foreach entry in the lockfile, create an entry object. We'll
// supplement/merge this with information from the package.json file
// in a moment...
//
const entries = Object.keys(yarn.object).map(key => makeYarnEntry(key, yarn.object[key]));
// Scan the node_modules directory and find all top-level ('foo') or scoped (@bar/baz)
// modules, i.e. folders which contain a package.json file...
const getModulesIn = p => fs.readdirSync(p).filter(f => isPackage(p, undefined, f));
const findScopes = p => fs.readdirSync(p).filter(f => f.startsWith("@") && fs.statSync(path.join(p, f)).isDirectory());
const getModulesInScope = (p, s) => fs.readdirSync(path.join(p, s)).filter(f => isPackage(p, s, f));
async _getPackageJson(context: string): Promise {
const packageJsonFile = path.join(context, 'package.json');
if (await exists(packageJsonFile)) {
const pkgJson = JSON.parse(await readFile(packageJsonFile, 'utf8'));
const yarnLockFile = path.join(context, 'yarn.lock');
let lockFile = null;
if (await exists(yarnLockFile)) {
const lockFileContent = await readFile(yarnLockFile, 'utf8');
// Needs to adjust line encoding on windows :(
// https://github.com/yarnpkg/yarn/issues/5214#issuecomment-368274679
const fixedLockFileContent = lockFileContent.replace(/\r/g, '');
const yarnLock = lockfile.parse(fixedLockFileContent);
lockFile = {
path: context,
dependencies: yarnLock.object,
};
}
return {
path: context,
dependencies: pkgJson.dependencies || {},
devDependencies: pkgJson.devDependencies || {},
peerDependencies: pkgJson.peerDependencies || {},
lockFile,
};
} else {
return undefined;
}
let moduleDirs = [
...getModulesIn('node_modules'),
...findScopes('node_modules').map(scope => getModulesIn(scope)).reduce((a, b) => a.concat(b), [])
]
let nestedModuleDirs = [];
moduleDirs.forEach(d => {
nestedModuleDirs.push(...getModulesIn(path.join(d, 'node_modules')));
nestedModuleDirs.push(...findScopes(path.join(d, 'node_modules')).map(scope => getModulesIn(scope)).reduce((a, b) => a.concat(b), []));
});
moduleDirs.push(...nestedModuleDirs);
const modules = moduleDirs.map(dir => parseNodeModulePackageJson(dir));
if (fs.existsSync('yarn.lock')) {
// Read the yarn.lock file and parse it.
const yarn = lockfile.parse(fs.readFileSync('yarn.lock', {encoding: 'utf8'}));
if (yarn.type !== 'success') {
throw new Error('Lockfile parse failed: ' + JSON.stringify(yarn, null, 2));
}
validateYarnLock(yarn, modules);
}
modules.forEach(module => flattenDependencies(module, module, modules));
let buildFile = generatedHeader + allFilegroup;
modules.forEach(module => buildFile += printNodeModule(module));
fs.writeFileSync('BUILD.bazel', buildFile);
}
library@>=1.0.0:
version "3.0.0"
resolved "https://example.net/library@^3.0.0"
library@>=1.1.0:
version "3.0.0"
resolved "https://example.net/library@^3.0.0"
library@^2.0.0:
version "2.1.0"
resolved "https://example.net/library@^2.1.0"
`;
const deduped = fixDuplicates(yarn_lock, {
useMostCommon: true,
});
const json = lockfile.parse(deduped).object;
expect(json['library@>=1.0.0']['version']).toEqual('2.1.0');
expect(json['library@>=1.1.0']['version']).toEqual('2.1.0');
expect(json['library@^2.0.0']['version']).toEqual('2.1.0');
const list = listDuplicates(yarn_lock, {
useMostCommon: true,
});
expect(list).toContain('Package "library" wants >=1.0.0 and could get 2.1.0, but got 3.0.0');
expect(list).toContain('Package "library" wants >=1.1.0 and could get 2.1.0, but got 3.0.0');
});
const parseYarnLock = file => lockfile.parse(file).object;
(packagePath: string = '.'): ILockfile => {
const file = fs.readFileSync(
path.join(root(), packagePath, 'yarn.lock'),
'utf8'
);
const lockfile = yarnLockfile.parse(file);
if (lockfile.type !== 'success') {
throw new Error(
`Could not parse lockfile: ${path.join(packagePath, 'yarn.lock')}!`
);
}
return lockfile.object;
}
);
function readYarn(basePath: string = '.') {
let file = fs.readFileSync(path.join(basePath, 'yarn.lock'), 'utf8');
let json = lockfile.parse(file);
if (json.type !== 'success') {
throw new Error('Error reading file');
}
return json.object;
}
private constructor(yarnLockContent: string) {
this.content = yarnLockContent
this.parsed = lockfile.parse(yarnLockContent)
}