Skip to content

Commit

Permalink
Improve compatibility (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Colman authored Oct 22, 2021
1 parent 9592ed5 commit 04c8c11
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 34 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 0.3.1

**Compatibility options**

`-D` | `--dev` save as dev dependency
`-E` | `--exact` save exact dependency
`-P` | `--peer`save as peer dependency

# 0.2.0

### New feature
Expand Down
36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
[<img alt="npm" src="https://img.shields.io/npm/dt/wksp?logo=npm">](https://npmjs.com/package/wksp)
<img alt="Maintained" src="https://img.shields.io/maintenance/yes/2022">

yarn workspaces extension
yarn **w**or**ksp**aces extension

> monorepo management tool
## Table of Contents:

Expand Down Expand Up @@ -31,7 +33,7 @@ npm i -g wksp
wksp <cmd>
```

> shorthand for: yarn workspace \<cmd>
> shorthand for: yarn **w**or**ksp**ace \<cmd>
> wksp will detect the package name automatically (if run inside the project folder)
Expand All @@ -41,9 +43,9 @@ if your current directory is the workspace root you can specify the name
wksp -n my-app <cmd>
```

> shorthand for: yarn workspace \<name> \<cmd>
> shorthand for: yarn **w**or**ksp**ace \<name> \<cmd>
**Variadic arguments**
### Variadic arguments

Variadic arguments are supported, (any arguments for the command)

Expand All @@ -52,7 +54,7 @@ wksp add react react-dom react-router
# cli cmd args...
```

**Passing options**
### Passing options

Options that are specific to yarn, require an `--` `--options`

Expand All @@ -68,12 +70,28 @@ wksp start -- --port 3002 --watch
# cli cmd -- script options
```

### Compatibility options

`-D` or `--dev` save as dev dependency

`-E` or `--exact` save exact dependency

`-P` or `--peer` save as peer dependency

some terminals doesn't allow passing options `--`

you can use compatibility options directly:

> `wksp add -D typescript`
<!-- ## Features
- alias
- wksp dev -->

---

## Examples

### root
Expand Down Expand Up @@ -107,3 +125,11 @@ wksp start

Have a great idea how we can extend yarn workspaces features ?
[suggest here](https://github.com/Andrew-Colman/wksp/issues/new)

<!-- @todo add list command -->

<!-- @todo add run all command -->

<!-- @todo package.json guide -->

<!-- @todo monorepo projects guide -->
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
{
"name": "wksp",
"version": "0.2.0",
"version": "0.3.1",
"main": "dist/index.js",
"source": "./src/index.ts",
"repository": "https://github.com/andrew-colman/wksp",
"author": {
"name": "Andrew Colman"
},
"keywords": [
"wksp",
"monorepo management",
"yarn",
"workspaces",
"workspace"
],
"license": "MIT",
"bin": {
"wksp": "dist/index.js"
},
"scripts": {
"prepublishOnly": "npm run build",
"build": "rollup --config rollup.config.js",
"dev": "sucrase-node ./src/index.ts ",
"test": "jest ",
Expand All @@ -22,7 +30,7 @@
"@rollup/plugin-node-resolve": "^13.0.4",
"@rollup/plugin-typescript": "^8.2.5",
"@types/jest": "^27.0.1",
"@types/node": "^16.3.1",
"@types/node": "^16.11.2",
"jest": "^27.0.6",
"rollup": "^2.56.2",
"rollup-plugin-preserve-shebang": "^1.0.1",
Expand Down
30 changes: 11 additions & 19 deletions src/program.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { Command, Argument } = require('commander');
import { getVersion, Result } from 'nanov';

const { Command } = require('commander');
import { verifyUpdates } from '@utils/verifyUpdates';
import { wksp } from './wksp';

require('jsonc-require');
Expand All @@ -12,38 +11,31 @@ const programVersion = 'wksp version: ' + currentVersion;
* (CLI TOOL) wksp
* @description yarn workspaces extension
* @param {string} options - options called from cmd
* @example wksp
* @example wksp --help
* @class Command
*/
const program = new Command();

program
.version(programVersion, '-v|--version')
.command('wksp', { isDefault: true })
.description('yarn workspace extension tool')

//
.option('-n, --name <name> ', 'project name (as in package.json)')
.argument('[command]', "package script you'd like to run")
.argument(
'[variadic...]',
"variadic arguments you'd like to pass to script"
)

//compatibility
.option('-D, --dev', 'save dev dependency')
.option('-P, --peer', 'save peer dependency')
.option('-E, --exact', 'save exact dependency')
//
.action((command: string, variadic: string[], options: any) =>
wksp(command, variadic, options)
wksp({ command, variadic, options })
);

//verify updates
program.hook('postAction', async () => {
const { latestVersion } = (await getVersion('wksp', currentVersion, {
cacheTime: 12,
})) as Result;

if (latestVersion) {
console.log('\n[wksp] new version available, run:');
console.log('npm i -g wksp\n');
console.log('to update');
}
});
program.hook('postAction', async () => verifyUpdates(currentVersion));

export { program };
13 changes: 13 additions & 0 deletions src/utils/verifyUpdates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getVersion, Result } from 'nanov';

export const verifyUpdates = async (currentVersion: string) => {
const { latestVersion } = (await getVersion('wksp', currentVersion, {
cacheTime: 12,
})) as Result;

if (latestVersion) {
console.log('\n[wksp] new version available, run:');
console.log('npm i -g wksp\n');
console.log('to update');
}
};
44 changes: 36 additions & 8 deletions src/wksp.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,58 @@
import path from 'path';
const execa = require('execa');

require('jsonc-require');

export async function wksp(
command: string,
variadic: string[],
options: { name: string }
) {
interface WKSP {
command: string;
variadic: string[];
options: WKSPOptions;
}

type WKSPOptions = {
name?: string;
} & CompatibilityOptions;

type CompatibilityOptions = {
dev?: boolean;
exact?: boolean;
peer?: boolean;
};

export async function wksp({ command, variadic, options }: WKSP) {
//if --list @todo
//if --all @todo
await workspace({ command, variadic, options });
}

async function workspace({ command, variadic, options }: WKSP) {
try {
const name = options.name ?? getPackageName();
const cmd = command ?? '';
const args = variadic ?? '';
const compatibility = getCompatibilityOptions(options);

execa('yarn', ['workspace', name, cmd, ...args], {
execa('yarn', ['workspace', name, cmd, ...args, ...compatibility], {
stdio: 'inherit',
}); //
} catch (error) {
console.log(error.message);
if (error instanceof Error) console.log(error.message);
}
}

//function workspaces(params: any) {}

function getPackageName() {
try {
return require(path.resolve(process.cwd(), './package.json')).name;
} catch (error) {
throw 'cant find package.json';
}
}

function getCompatibilityOptions(options: CompatibilityOptions): string[] {
return [
options.dev && '-D',
options.exact && '-E',
options.peer && '-P',
].filter(Boolean) as string[];
}

0 comments on commit 04c8c11

Please sign in to comment.