Skip to content

Commit

Permalink
feat(tooling-deps): add cli subcommand/nx executor for project deps
Browse files Browse the repository at this point in the history
[Finishes #186638343]
  • Loading branch information
ianwremmel committed Feb 2, 2024
1 parent 43181a6 commit c241cf2
Show file tree
Hide file tree
Showing 22 changed files with 945 additions and 318 deletions.
556 changes: 356 additions & 200 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion packages/@code-like-a-carpenter/cli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
"publishConfig": {
"access": "public"
},
"bin": "./cli.mjs"
"bin": "./cli.mjs",
"devDependencies": {
"@types/yargs": "^17.0.32"
}
}
7 changes: 5 additions & 2 deletions packages/@code-like-a-carpenter/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
"bin": "./cli.mjs",
"code-like-a-carpenter": {
"plugins": [
"@code-like-a-carpenter/cli-plugin-example"
"@code-like-a-carpenter/cli-plugin-example",
"@code-like-a-carpenter/tooling-deps"
]
},
"dependencies": {
"@code-like-a-carpenter/cli-core": "*"
"@code-like-a-carpenter/cli-core": "*",
"@code-like-a-carpenter/cli-plugin-example": "*",
"@code-like-a-carpenter/tooling-deps": "*"
},
"exports": {
".": {
Expand Down
6 changes: 6 additions & 0 deletions packages/@code-like-a-carpenter/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@
{
"path": "../cli-core",
},
{
"path": "../cli-plugin-example",
},
{
"path": "../tooling-deps",
},
],
}
5 changes: 4 additions & 1 deletion packages/@code-like-a-carpenter/foundation-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@graphql-tools/schema": "^10.0.2",
"glob": "^10.3.10",
"graphql": "^16.8.1",
"yargs": "^17.7.1",
"yargs": "^17.7.2",
"zod": "^3.22.4"
},
"bin": "./cli.mjs",
Expand All @@ -35,5 +35,8 @@
"import": "./dist/esm/index.mjs"
},
"./package.json": "./package.json"
},
"devDependencies": {
"@types/yargs": "^17.0.32"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.133",
"@types/js-yaml": "^4.0.9"
}
}
7 changes: 3 additions & 4 deletions packages/@code-like-a-carpenter/nx-auto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
"dependencies": {
"depcheck": "^1.4.3",
"glob": "^10.3.10",
"prettier": "^3.0.3",
"remark": "^14.0.2",
"remark-stringify": "^10.0.2",
"remark-toc": "^8.0.1",
"typescript": "^5.3.3",
"unist-builder": "^3.0.1",
"yargs": "^17.7.1",
"prettier": "^3.0.3"
"yargs": "^17.7.2"
},
"devDependencies": {
"@schemastore/package": "^0.0.8",
"@schemastore/tsconfig": "^0.0.10",
"@types/prettier": "^3.0.0",
"@types/yargs": "^17.0.22"
"@types/prettier": "^3.0.0"
},
"private": true
}
109 changes: 0 additions & 109 deletions packages/@code-like-a-carpenter/nx-auto/src/commands/package.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict';

const assert = require('assert');
const {spawnSync} = require('child_process');
const fs = require('fs');
const path = require('path');

const depcheck = require('depcheck');

const {writePackageJson} = require('../lib/helpers');
const {pathToPackage} = require('../lib/helpers');
const {readPackageJson} = require('../lib/helpers');

/** @type import('yargs').CommandModule */
Expand All @@ -34,7 +30,6 @@ const command = {
`packageName must not end with package.json, got ${packageName}`
);

await deps(packageName);
if (type === 'example') {
await configExample(packageName);
} else {
Expand Down Expand Up @@ -149,107 +144,3 @@ async function loadRootPackageJson() {
)
);
}

/**
* @param {string} packageName
*/
async function deps(packageName) {
const packagePath = pathToPackage(packageName);

const results = await depcheck(packagePath, {
detectors: [
...Object.values(depcheck.detector),
depcheck.detector.typescriptImportType,
],
ignoreDirs: ['.aws-sam', 'dist'],
});

if (results.dependencies.length > 0) {
await removeExtraDependencies(packagePath, results.dependencies);
}

const missingLocalPackages = Object.keys(results.missing).filter(
(m) => m.startsWith('@code-like-a-carpenter') && m !== packageName
);
const missingNodePackages = Object.keys(results.missing).filter(
(m) => !m.startsWith('@code-like-a-carpenter')
);

if (missingLocalPackages.length > 0) {
await addMissingLocalDependencies(packagePath, missingLocalPackages);
}

if (missingNodePackages.length > 0) {
await addMissingNodeDependencies(packagePath, missingNodePackages);
}
}

/**
* @param {string} packageName
* @param {readonly string[]} dependencies
*/
function addMissingLocalDependencies(packageName, dependencies) {
spawnSync('npm', ['install', '--workspace', packageName, ...dependencies], {
stdio: 'inherit',
});
}

// This is the current version bundled with lambda
const awsSdkVersion = '3.188.0';

/**
* @param {string} packageName
* @param {readonly string[]} dependencies
*/
function addMissingNodeDependencies(packageName, dependencies) {
const awsDeps = dependencies
.filter((d) => d.startsWith('@aws-sdk'))
.map((d) => `${d}@${awsSdkVersion}`);

const normalDeps = dependencies
.filter((d) => !d.startsWith('@aws-sdk'))
.filter((d) => d !== 'aws-lambda')
.map((d) => `${d}@latest`);

const needsLambda = dependencies.includes('aws-lambda');

if (awsDeps.length) {
spawnSync(
'npm',
['install', '--workspace', packageName, '--save-exact', ...awsDeps],
{
stdio: 'inherit',
}
);
}

if (normalDeps.length) {
spawnSync('npm', ['install', '--workspace', packageName, ...normalDeps], {
stdio: 'inherit',
});
}

if (needsLambda) {
spawnSync(
'npm',
[
'install',
'--save-dev',
'--workspace',
packageName,
'@types/aws-lambda',
],
{stdio: 'inherit'}
);
}
}

/**
* @param {string} packageName
* @param {readonly string[]} dependencies
*/
function removeExtraDependencies(packageName, dependencies) {
spawnSync('npm', ['uninstall', '--workspace', packageName, ...dependencies], {
stdio: 'inherit',
});
}
10 changes: 9 additions & 1 deletion packages/@code-like-a-carpenter/nx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ export const createNodes: CreateNodes = [
],
executor: 'nx:noop',
},
'codegen:deps': {
cache: true,
executor: '@code-like-a-carpenter/tooling-deps:deps',
options: {
definitelyTyped: ['yargs'],
packageName: projectName,
},
},
'codegen:executors': {
cache: true,
executor: '@code-like-a-carpenter/nx:json-schema',
Expand Down Expand Up @@ -164,7 +172,7 @@ fi
},
'codegen:project-refs': {
cache: true,
dependsOn: ['^codegen:project-refs', 'codegen:package'],
dependsOn: ['^codegen:project-refs', 'codegen:deps'],
executor: 'nx:run-commands',
inputs: ['{projectRoot}/package.json', 'sharedGlobals'],
options: {
Expand Down
34 changes: 34 additions & 0 deletions packages/@code-like-a-carpenter/tooling-deps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @code-like-a-carpenter/tooling-deps

[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)

> A CLI plugin for Code Like a Carpenter to manage dependencies
## Table of Contents

- [Install](#install)
- [Usage](#usage)
- [Maintainer](#maintainer)
- [Contributing](#contributing)
- [License](#license)

## Install

```bash
npm i @code-like-a-carpenter/tooling-deps
```

## Usage

## Maintainer

[Ian Remmel](https://www.ianwremmel.com)

## Contributing

Please see contributing guidelines at the
[project homepage](https://www.github.com/code-like-a-carpenter/workbench/).

## License

MIT © [Ian Remmel](https://www.ianwremmel.com) 2023 until at least now
9 changes: 9 additions & 0 deletions packages/@code-like-a-carpenter/tooling-deps/executors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"executors": {
"deps": {
"implementation": "./executors/deps/executor",
"schema": "./executors/deps/schema.json",
"description": "deps executor"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type {Executor} from '@nx/devkit';

// eslint-disable-next-line workspaces/no-absolute-imports
import {main} from '@code-like-a-carpenter/tooling-deps';

import type {DepsExecutor} from './schema';

const runExecutor: Executor<DepsExecutor> = async (options) => {
await main(options);
return {
success: true,
};
};

export default runExecutor;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/

export interface DepsExecutor {
awsSdkVersion?: string;
devPatterns?: string[];
dryRun?: boolean;
ignoreDirs?: string[];
packageName: string;
definitelyTyped?: string[];
[k: string]: unknown;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json-schema.org/schema",
"version": 2,
"title": "Deps executor",
"description": "",
"type": "object",
"properties": {
"awsSdkVersion": {
"default": "3.188.0",
"type": "string"
},
"devPatterns": {
"default": ["*.spec.[jt]sx?"],
"items": {
"type": "string"
},
"type": "array"
},
"dryRun": {
"default": false,
"type": "boolean"
},
"ignoreDirs": {
"default": [".aws-sam", "dist", "node_modules", "build", "public/build"],
"items": {
"type": "string"
},
"type": "array"
},
"packageName": {
"type": "string"
},
"definitelyTyped": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": ["packageName"]
}
Loading

0 comments on commit c241cf2

Please sign in to comment.