Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: "yalc add" flags #68

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
{
"taskName": "tsc",
"command": "node",
"args": [
"./node_modules/typescript/lib/tsc.js",
"-w",
"-p",
"."
],
"args": ["./node_modules/typescript/lib/tsc.js", "-w", "-p", "."],
"isBackground": true,
"problemMatcher": "$tsc-watch"
}]
}
}
]
}
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
"scripts": {
"build": "tsc",
"clean": "trash **/*.js **/*.js.map **/*.d.ts **/*.log !**/node_modules/**",
"precommit": "yalc check",
"prepublishOnly": "yarn clean && tsc && yarn test",
"test": "tsc && mocha test && yarn lint",
"watch": "mocha test --watch",
"ci": "tsc && yarn test",
"lint": "tslint -p ."
},
"husky": {
"hooks": {
"pre-commit": "yalc check && pretty-quick --staged"
}
},
"dependencies": {
"del": "^2.2.2",
"fs-extra": "^4.0.2",
Expand All @@ -44,11 +48,11 @@
"@types/npm-packlist": "^1.1.0",
"@types/yargs": "^6.6.0",
"clean-ts-built": "^1.0.0",
"husky": "^0.13.3",
"husky": "^1.3.1",
"mocha": "^5.2.0",
"prettier": "^1.14.2",
"pretty-quick": "^1.8.0",
"trash-cli": "^1.4.0",
"ts-clean-built": "^1.0.0",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0",
"tslint-plugin-prettier": "^1.3.0",
Expand Down
31 changes: 15 additions & 16 deletions src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ const ensureSymlinkSync = fs.ensureSymlinkSync as typeof fs.symlinkSync
export interface AddPackagesOptions {
dev?: boolean
link?: boolean
linkDep?: boolean
yarn?: boolean
safe?: boolean
pure?: boolean
noSave?: boolean
workingDir: string
}

Expand Down Expand Up @@ -71,7 +70,8 @@ export const addPackages = async (
return
}
const doPure =
options.pure === false ? false : options.pure || !!localPkg.workspaces
options.pure !== undefined ? options.pure : !!localPkg.workspaces

const addedInstalls = packages
.map(packageName => {
const { name, version = '' } = parsePackageName(packageName)
Expand Down Expand Up @@ -127,19 +127,15 @@ export const addPackages = async (
}
if (!doPure) {
const destModulesDir = join(workingDir, 'node_modules', name)
if (options.link || options.linkDep || isSymlink(destModulesDir)) {
fs.removeSync(destModulesDir)
}

if (options.link || options.linkDep) {
fs.removeSync(destModulesDir)
if (options.link) {
ensureSymlinkSync(destYalcCopyDir, destModulesDir, 'junction')
} else {
emptyDirExcludeNodeModules(destModulesDir)
fs.copySync(destYalcCopyDir, destModulesDir)
}

if (!options.link) {
const protocol = options.linkDep ? 'link:' : 'file:'
if (!options.noSave) {
const protocol = options.link ? 'link:' : 'file:'
const localAddress =
protocol + values.yalcPackagesFolder + '/' + pkg.name

Expand Down Expand Up @@ -174,9 +170,12 @@ export const addPackages = async (
replacedVersion =
replacedVersion == localAddress ? '' : replacedVersion
}
const addedAction = options.link ? 'linked' : 'added'

const addedAction = options.noSave ? 'linked' : 'added'
console.log(
`Package ${pkg.name}@${pkg.version} ${addedAction} ==> ${destModulesDir}.`
`Package ${pkg.name}@${
pkg.version
} ${addedAction} ==> ${destModulesDir}.`
)
}

Expand All @@ -202,8 +201,8 @@ export const addPackages = async (
version: i!.version,
replaced: i!.replaced,
pure: doPure,
file: !options.link && !options.linkDep && !doPure,
link: options.linkDep && !doPure,
file: !options.link && !doPure,
link: options.link && !doPure,
signature: i.signature
})),
{ workingDir: options.workingDir }
Expand All @@ -213,6 +212,6 @@ export const addPackages = async (

if (options.yarn) {
console.log('Running yarn:')
execSync('yarn', {cwd: options.workingDir})
execSync('yarn', { cwd: options.workingDir })
}
}
12 changes: 5 additions & 7 deletions src/installations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ export const readInstallationsFile = (): InstallationsFile => {
export const showInstallations = ({ packages }: { packages: string[] }) => {
const config = readInstallationsFile()
Object.keys(config)
.filter(
packageName =>
packages.length ? packages.indexOf(packageName) >= 0 : true
.filter(packageName =>
packages.length ? packages.indexOf(packageName) >= 0 : true
)
.map((name: PackageName) => ({ name, locations: config[name] }))
.forEach(({ name, locations }) => {
Expand All @@ -55,14 +54,13 @@ export const cleanInstallations = async ({
packages,
dry
}: {
packages: string[],
packages: string[]
dry: boolean
}) => {
const config = readInstallationsFile()
const installsToRemove = Object.keys(config)
.filter(
packageName =>
packages.length ? packages.indexOf(packageName) >= 0 : true
.filter(packageName =>
packages.length ? packages.indexOf(packageName) >= 0 : true
)
.map((name: PackageName) => ({ name, locations: config[name] }))
.reduce(
Expand Down
5 changes: 3 additions & 2 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ export const updatePackages = async (
await addPackages(packagesLinks, {
workingDir: options.workingDir,
link: true,
noSave: true,
pure: false
})

const packagesLinkDep = lockPackages.filter(p => p.link).map(p => p.name)
await addPackages(packagesLinkDep, {
workingDir: options.workingDir,
linkDep: true,
workingDir,
link: true,
pure: false
})

Expand Down
12 changes: 7 additions & 5 deletions src/yalc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ yargs
describe: 'Add package from yalc repo to the project',
builder: () => {
return yargs
.default('yarn', false)
.boolean(['file', 'dev', 'save-dev', 'link', 'yarn', 'pure'])
.boolean(['link', 'dev', 'save', 'yarn', 'pure'])
.default('link', true)
.default('save', true)
.help(true)
},
handler: argv => {
Expand All @@ -132,10 +133,10 @@ yargs
false
)
return addPackages(argv._.slice(1), {
dev: argv.dev || argv.saveDev,
yarn: argv.yarn,
linkDep: argv.link,
dev: argv.dev,
link: argv.link,
pure: hasPureArg ? argv.pure : undefined,
noSave: !argv.save,
workingDir: process.cwd()
})
}
Expand All @@ -149,6 +150,7 @@ yargs
handler: argv => {
return addPackages(argv._.slice(1), {
link: true,
noSave: true,
workingDir: process.cwd()
})
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/project/nested/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"dependencies": {
"dep-package": "1.0.0"
}
}
}
14 changes: 7 additions & 7 deletions test/fixture/project/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "yalc-test-project",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"dep-package": "1.0.0"
}
"name": "yalc-test-project",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"dep-package": "1.0.0"
}
}
2 changes: 1 addition & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ describe('Yalc package manager', function() {
before(() => {
return addPackages([values.depPackage], {
workingDir: projectDir,
linkDep: true
link: true
})
})
it('copies package to .yalc folder', () => {
Expand Down
Loading