Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
iansu committed Apr 4, 2020
0 parents commit f6fac42
Show file tree
Hide file tree
Showing 15 changed files with 3,913 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# http://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 100
trim_trailing_whitespace = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false

[COMMIT_EDITMSG]
max_line_length = 0
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "eslint-config-neo/config-backend",
"rules": {
"no-console": "off",
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }]
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build
node_modules

*.log
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"printWidth": 100,
"singleQuote": true
}
1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save-prefix ""
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Ian Sutherland

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Blarn

A Yarn wrapper with extra functionality

## Features

- Automatically add and remove TypeScript `@types` packages when adding or removing packages in a TypeScript project

## Usage

### Requirements

- You must have Yarn 1 installed and available on your path

### Installation

```sh
// npm
npm install -g blarn

// yarn
yarn global add blarn
```

### Upgrading

```sh
// npm
npm install -g blarn

// yarn
yarn global upgrade blarn --latest
```

### Running Yarn commands

Blarn will pass all commands and arguments through to Yarn. When adding packages in a TypeScript project it will execute a second `yarn add` command to add any available `@types` packages as dev dependencies. When removing packages any corresponding `@types` packages will be added to the list of packages to remove.

### Examples

#### Adding packages

`blarn add yargs`

If you run this command in a TypeScript project `yargs` will be installed as a dependency and `@types/yargs` will be installed as a dev dependency. If you run this command in a JavaScript project only `yargs` will be installed as a dependency.

### Removing packages

`blarn remove yargs`

If you run this command in a TypeScript project and `@types/yargs` exists in `package.json` both `yargs` and `@types/yargs` will be removed. If you run this command in a JavaScript project only `yargs` will be removed.

## Contributing

### Running locally

1. Fork and clone this repo
1. Run `yarn`
1. Run `yarn build` or `yarn watch`
1. Run `yarn start` or `bin/blarn.js`

## Acknowledgements

This project was inspired by [Yarn 2](https://yarnpkg.com/) and [Bojack Horseman](https://bojackhorseman.fandom.com/wiki/Diane_Nguyen).
5 changes: 5 additions & 0 deletions bin/blarn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

const app = require('../build').default;

app();
73 changes: 73 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "blarn",
"description": "A Yarn wrapper with extra functionality",
"version": "1.0.0",
"author": "Ian Sutherland <ian@iansutherland.ca>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/iansu/blarn"
},
"engines": {
"node": ">=12.0.0"
},
"bin": {
"blarn": "bin/blarn.js"
},
"scripts": {
"start": "bin/blarn.js",
"build": "NODE_ENV=production ncc build src/app.ts -o build --minify",
"watch": "NODE_ENV=production ncc build src/app.ts -o build --minify --watch",
"clean": "rimraf build",
"lint": "eslint \"**/*.{ts,js}\"",
"format": "prettier --write \"**/*.{ts,js,json,yaml,yml,md}\"",
"format:check": "prettier --debug-check \"**/*.{ts,js,json,yaml,yml,md}\"",
"prepublishOnly": "rimraf build && ncc build src/app.ts -o build --minify"
},
"files": [
"bin",
"build"
],
"keywords": [
"yarn",
"npm",
"typescript",
"cli",
"package manager"
],
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{ts,js}": [
"eslint"
],
"*.{ts,js,json,yaml,yml,md}": [
"prettier --write"
]
},
"dependencies": {
"update-notifier": "4.1.0"
},
"devDependencies": {
"@types/find-package-json": "1.1.1",
"@types/node": "13.11.0",
"@types/pacote": "11.1.0",
"@types/update-notifier": "4.1.0",
"@zeit/ncc": "0.22.0",
"chalk": "4.0.0",
"eslint": "6.8.0",
"eslint-config-neo": "0.5.2",
"execa": "4.0.0",
"find-package-json": "1.2.0",
"husky": "4.2.3",
"lint-staged": "10.1.1",
"pacote": "11.1.4",
"prettier": "1.19.1",
"rimraf": "3.0.2",
"ts-node": "8.8.1",
"typescript": "3.8.3"
}
}
139 changes: 139 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import execa from 'execa';
import pacote from 'pacote';
import findPackageJson from 'find-package-json';
import updateNotifier from 'update-notifier';

interface PackageJson {
name: string;
version: string;
devDependencies?: {
[key: string]: string;
};
}

const getPackageJson = (startDirectory: string): PackageJson => {
const finder = findPackageJson(startDirectory);
const results = finder.next();

if (results.filename) {
return { name: '', version: '1.0.0', ...results.value };
} else {
throw new Error('Unable to find package.json');
}
};

const printVersion = async (): Promise<void> => {
const packageJson = getPackageJson(__dirname);
const { stdout: yarnVersion } = await execa('yarn', ['--version']);

console.log(`Blarn version: ${packageJson.version}`);
console.log(`Yarn version: ${yarnVersion}`);
};

const isYarn1 = async (): Promise<boolean> => {
const { stdout } = await execa('yarn', ['--version']);

return stdout.startsWith('1.');
};

const isTypeScriptProject = (packageJson: PackageJson): boolean => {
if (
packageJson.devDependencies &&
Object.keys(packageJson.devDependencies).includes('typescript')
) {
return true;
} else {
return fs.existsSync(path.join(process.cwd(), 'tsconfig.json'));
}
};

const runYarn = async (...args: string[]): Promise<void> => {
try {
await execa('yarn', args, { stdio: 'inherit' });
} catch (error) {
process.exit(error.exitCode);
}
};

const app = async (): Promise<void> => {
updateNotifier({ pkg: getPackageJson(__dirname) }).notify();

if (process.argv[2] === '--version' || process.argv[2] === '-v') {
await printVersion();

return;
}

if (!(await isYarn1())) {
console.error(`${chalk.red('error')} blarn in not compatible with Yarn 2`);

return;
}

const packageJson = getPackageJson(process.cwd());

if (process.argv[2] === 'remove' && isTypeScriptProject(packageJson)) {
const typePackages = [];

if (packageJson.devDependencies) {
for (const pkg of process.argv.slice(3)) {
if (!pkg.startsWith('-') && !pkg.startsWith('--') && !pkg.startsWith('@types')) {
let typePackage = `@types/${pkg}`;

if (pkg.startsWith('@')) {
const [org, packageName] = pkg.slice(1).split('/');

typePackage = `@types/${org}__${packageName}`;
}

if (Object.keys(packageJson.devDependencies).includes(typePackage)) {
typePackages.push(typePackage);
}
}
}
}

await runYarn('remove', ...process.argv.slice(3), ...typePackages);

return;
}

await runYarn(...process.argv.slice(2));

if (process.argv[2] === 'add' && isTypeScriptProject(packageJson)) {
const typePackages = [];

for (const pkg of process.argv.slice(3)) {
if (!pkg.startsWith('-') && !pkg.startsWith('--') && !pkg.startsWith('@types')) {
let typePackage = `@types/${pkg}`;

if (pkg.startsWith('@')) {
const [org, packageName] = pkg.slice(1).split('/');

typePackage = `@types/${org}__${packageName}`;
}

try {
await pacote.manifest(typePackage);

typePackages.push(typePackage);
} catch (error) {
if (error.code !== 'E404') {
console.error(`Error finding package: ${typePackage}`);
}
}
}
}

if (typePackages.length > 0) {
console.log(`\n${chalk.blue('info')} Installing types: ${typePackages.join(' ')}`);

await runYarn('add', '--dev', ...typePackages);
}
}
};

export default app;
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"strict": true,
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"declaration": true,
"outDir": "build",
"baseUrl": ".",
"paths": {
"*": ["*", "node_modules/*", "src/*"]
}
},
"include": ["src/**/*"]
}
Loading

0 comments on commit f6fac42

Please sign in to comment.