Skip to content

Commit

Permalink
replace regen-deps with deps regen
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Oct 10, 2023
1 parent 49a77ad commit c347e6d
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 118 deletions.
47 changes: 35 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ Pass any extra tsc flags to this command.

Run type checking without emitting compiled files.

## deps

Either upgrade or check dependencies

### check:

Check that dependencies are all good via a default set of rules in configs/dep-cruiser.config.ts and dependency-cruiser.

### upgrade:

Upgrade all packages using npm-check-updates and configs/ncu.config.ts.

### regen:

Regenerate all npm deps and package-lock.json

### Examples

- Upgrade deps: `virmator deps upgrade`
- Check deps: `virmator deps check`
- Check deps for a specific file: `virmator deps check ./src/my-file.ts`
- Regen deps: `virmator deps regen`

### check

validate dependencies

### upgrade

upgrade npm dependencies

### regen

regenerate npm dependencies

## docs

This also inserts code snippets into markdown files. This uses the markdown-code-example-inserter package to expand code link comments inside of markdown files to actual markdown code blocks. See that package's README for more details but the basics are that you need a comment that looks like the following in your markdown file for this to do anything: `<!-- example-link: path/to/file.ts -->`
Expand Down Expand Up @@ -131,10 +166,6 @@ Publish an npm package. Includes workspace support. Add test commands to run bef
- publish a package: `virmator publish`
- publish a package and run tests beforehand: `virmator publish npm test`

## regen-deps

Regenerates all npm deps.

## spellcheck

Spellcheck code with cspell. By default this spellchecks every file in the entire repo (except for those ignored in the config file), including .dot files. If any arguments are passed to this command, the default cspell args that this command applies are ignored, you'll have to supply them via your args.
Expand Down Expand Up @@ -175,11 +206,3 @@ run web tests with code coverage calculations
## update-configs

Update all existing configuration files that virmator is able to update. (Like base config files.)

## upgrade-deps

Upgrade all packages using npm-check-updates and config/ncu.config.ts.

### Examples

- Run command: `virmator upgrade-deps`
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "virmator",
"version": "8.0.5",
"version": "9.0.0",
"description": "Handle common package configs, commands, and dependencies.",
"keywords": [
"automation",
Expand Down
81 changes: 79 additions & 2 deletions src/commands/deps.command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {isLengthAtLeast} from '@augment-vir/common';
import {readPackageJson} from '@augment-vir/node-js';
import {isLengthAtLeast, isTruthy} from '@augment-vir/common';
import {ColorKey, readDirRecursive, readPackageJson, toLogString} from '@augment-vir/node-js';
import {existsSync} from 'fs';
import {unlink} from 'fs/promises';
import {join} from 'path';
import {defineCommand} from '../api/command/define-command';
import {NpmDepTypeEnum} from '../api/command/define-command-inputs';
import {removeDirectory} from '../augments/fs';
import {compileTs, withTypescriptConfigFile} from '../augments/typescript-config-file';
import {getNpmBinPath, virmatorConfigsDir} from '../file-paths/package-paths';

Expand All @@ -13,6 +15,7 @@ export const depsCommandDefinition = defineCommand(
subCommandDescriptions: {
check: 'validate dependencies',
upgrade: 'upgrade npm dependencies',
regen: 'regenerate npm dependencies',
},
configFiles: {
depCruiserConfig: {
Expand Down Expand Up @@ -45,6 +48,10 @@ export const depsCommandDefinition = defineCommand(
title: 'upgrade',
content: `Upgrade all packages using npm-check-updates and ${configFiles.ncuConfig.copyToPathRelativeToRepoDir}.`,
},
{
title: 'regen',
content: 'Regenerate all npm deps and package-lock.json',
},
],

examples: [
Expand All @@ -60,6 +67,10 @@ export const depsCommandDefinition = defineCommand(
title: 'Check deps for a specific file',
content: `${packageBinName} ${commandName} ${subCommands.check} ./src/my-file.ts`,
},
{
title: 'Regen deps',
content: `${packageBinName} ${commandName} ${subCommands.regen}`,
},
],
};
},
Expand All @@ -72,6 +83,7 @@ export const depsCommandDefinition = defineCommand(
packageDir,
configFiles,
filteredInputArgs,
logging,
}) => {
const subCommandsErrorString = allAvailableSubCommands.join(',');

Expand Down Expand Up @@ -134,6 +146,71 @@ export const depsCommandDefinition = defineCommand(

return {success: true};
});
} else if (inputSubCommands[0] === subCommands.regen) {
const allChildNodeModules = await readDirRecursive(repoDir);
const nodeModulesPaths = Array.from(
new Set(
allChildNodeModules
.filter((path) => path.includes('node_modules'))
.map((path) =>
path.replace(/([\\\/]|^)node_modules[\\\/].+$/, '$1node_modules'),
),
),
);

const packageLockPath = join(repoDir, 'package-lock.json');

const removePackageLock = existsSync(packageLockPath);

const logLines = [
'Removing node_modules:',
...nodeModulesPaths.map((nodeModulesPath) => ` ${nodeModulesPath}`),
removePackageLock ? '\nRemoving package-lock.json' : '',
].filter(isTruthy);

logging.stdout(
toLogString({
args: [
logLines.join('\n'),
],
colors: ColorKey.faint,
}),
);

let errorsHappened = false;

if (removePackageLock) {
try {
await unlink(packageLockPath);
} catch (error) {
errorsHappened = true;
logging.stderr('Failed to remove package-lock.json.');
}
}

await Promise.all(
nodeModulesPaths.map(async (nodeModulesPath) => {
try {
await removeDirectory(nodeModulesPath);
} catch (error) {
errorsHappened = true;
logging.stderr('Failed to remove package-lock.json.');
}
}),
);

if (errorsHappened) {
return {
success: false,
};
}

return {
args: [
'npm',
'i',
],
};
} else {
throw new Error(
`Invalid sub command for '${commandName}' given: got '${inputSubCommands[0]}' but expected one of ${subCommandsErrorString}`,
Expand Down
3 changes: 1 addition & 2 deletions src/commands/frontend.command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {MaybePromise, isTruthy} from '@augment-vir/common';
import {randomString} from '@augment-vir/node-js';
import {MaybePromise, isTruthy, randomString} from '@augment-vir/common';
import {unlink} from 'fs/promises';
import {dirname, join, resolve} from 'path';
import type {UserConfig} from 'vite';
Expand Down
93 changes: 0 additions & 93 deletions src/commands/regen-deps.command.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {formatCommandDefinition} from './commands/format.command';
import {frontendCommandDefinition} from './commands/frontend.command';
import {initCommandDefinition} from './commands/init.command';
import {publishCommandDefinition} from './commands/publish.command';
import {regenDepsCommandDefinition} from './commands/regen-deps.command';
import {spellcheckCommandDefinition} from './commands/spellcheck.command';
import {testWebCommandDefinition} from './commands/test-web.command';
import {testCommandDefinition} from './commands/test.command';
Expand Down Expand Up @@ -39,7 +38,6 @@ export const virmator = createVirmator({
frontendCommandDefinition,
initCommandDefinition,
publishCommandDefinition,
regenDepsCommandDefinition,
spellcheckCommandDefinition,
testCommandDefinition,
testWebCommandDefinition,
Expand Down
8 changes: 4 additions & 4 deletions test-files/test-expectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
"dir": "deps/valid",
"exitCode": 1,
"key": "multi-sub-command-fail",
"stderr": "Too many sub commands given to 'deps' command, only one is allowed from: check,upgrade",
"stderr": "Too many sub commands given to 'deps' command, only one is allowed from: check,upgrade,regen",
"stdout": "Installing esbuild... Installing npm-check-updates... Installing dependency-cruiser... running deps... Successfully copied dep-cruiser.config.ts Successfully copied ncu.config.ts"
},
"no-sub-command-fail": {
"dir": "deps/valid",
"exitCode": 1,
"key": "no-sub-command-fail",
"stderr": "Missing sub command for 'deps' command: expected one of check,upgrade",
"stderr": "Missing sub command for 'deps' command: expected one of check,upgrade,regen",
"stdout": "Installing esbuild... Installing npm-check-updates... Installing dependency-cruiser... running deps... Successfully copied dep-cruiser.config.ts Successfully copied ncu.config.ts"
},
"npn-npm-dep-fail": {
Expand Down Expand Up @@ -195,14 +195,14 @@
"exitCode": 0,
"key": "basic-init",
"stderr": "",
"stdout": "Installing prettier... Installing prettier-plugin-jsdoc... Installing prettier-plugin-multiline-arrays... Installing prettier-plugin-organize-imports... Installing prettier-plugin-packagejson... Installing prettier-plugin-sort-json... Installing prettier-plugin-toml... Installing prettier-plugin-interpolated-html-tags... running init... Successfully copied .gitattributes Successfully copied .gitignore Successfully copied .npmignore Successfully copied .nvmrc Successfully copied .prettierignore Successfully copied .prettierrc.js Successfully copied build-for-gh-pages.yml Successfully copied cspell.config.js Successfully copied index.css Successfully copied index.html Successfully copied LICENSE-CC0 Successfully copied LICENSE-MIT Successfully copied mocha.config.js Successfully copied ncu.config.ts Successfully copied nyc.config.js Successfully copied package.json Successfully copied settings.json Successfully copied tagged-release.yml Successfully copied tests.yml Successfully copied tsconfig.json Successfully copied typedoc.config.ts Successfully copied vir-app.element.ts Successfully copied vite.config.ts Successfully copied web-test-runner.config.mjs init succeeded."
"stdout": "Installing prettier... Installing prettier-plugin-jsdoc... Installing prettier-plugin-multiline-arrays... Installing prettier-plugin-organize-imports... Installing prettier-plugin-packagejson... Installing prettier-plugin-sort-json... Installing prettier-plugin-toml... Installing prettier-plugin-interpolated-html-tags... running init... Successfully copied .gitattributes Successfully copied .gitignore Successfully copied .npmignore Successfully copied .nvmrc Successfully copied .prettierignore Successfully copied .prettierrc.js Successfully copied build-for-gh-pages.yml Successfully copied cspell.config.js Successfully copied dep-cruiser.config.ts Successfully copied index.css Successfully copied index.html Successfully copied LICENSE-CC0 Successfully copied LICENSE-MIT Successfully copied mocha.config.js Successfully copied ncu.config.ts Successfully copied nyc.config.js Successfully copied package.json Successfully copied settings.json Successfully copied tagged-release.yml Successfully copied tests.yml Successfully copied tsconfig.json Successfully copied typedoc.config.ts Successfully copied vir-app.element.ts Successfully copied vite.config.ts Successfully copied web-test-runner.config.mjs init succeeded."
},
"init with files to upgrade": {
"dir": "init/files-for-update",
"exitCode": 0,
"key": "init with files to upgrade",
"stderr": "",
"stdout": "Installing prettier... Installing prettier-plugin-jsdoc... Installing prettier-plugin-multiline-arrays... Installing prettier-plugin-organize-imports... Installing prettier-plugin-packagejson... Installing prettier-plugin-sort-json... Installing prettier-plugin-toml... Installing prettier-plugin-interpolated-html-tags... running init... Successfully copied .gitattributes Successfully copied .gitignore Successfully copied .npmignore Successfully copied .nvmrc Successfully copied .prettierignore Successfully copied .prettierrc.js Successfully copied build-for-gh-pages.yml Successfully copied cspell.config.js Successfully copied index.css Successfully copied index.html Successfully copied LICENSE-CC0 Successfully copied LICENSE-MIT Successfully copied mocha.config.js Successfully copied ncu.config.ts Successfully copied nyc.config.js Successfully copied package.json Successfully copied settings.json Successfully copied tagged-release.yml Successfully copied tests.yml Successfully copied tsconfig.json Successfully copied typedoc.config.ts Successfully copied vir-app.element.ts Successfully copied vite.config.ts Successfully copied web-test-runner.config.mjs init succeeded."
"stdout": "Installing prettier... Installing prettier-plugin-jsdoc... Installing prettier-plugin-multiline-arrays... Installing prettier-plugin-organize-imports... Installing prettier-plugin-packagejson... Installing prettier-plugin-sort-json... Installing prettier-plugin-toml... Installing prettier-plugin-interpolated-html-tags... running init... Successfully copied .gitattributes Successfully copied .gitignore Successfully copied .npmignore Successfully copied .nvmrc Successfully copied .prettierignore Successfully copied .prettierrc.js Successfully copied build-for-gh-pages.yml Successfully copied cspell.config.js Successfully copied dep-cruiser.config.ts Successfully copied index.css Successfully copied index.html Successfully copied LICENSE-CC0 Successfully copied LICENSE-MIT Successfully copied mocha.config.js Successfully copied ncu.config.ts Successfully copied nyc.config.js Successfully copied package.json Successfully copied settings.json Successfully copied tagged-release.yml Successfully copied tests.yml Successfully copied tsconfig.json Successfully copied typedoc.config.ts Successfully copied vir-app.element.ts Successfully copied vite.config.ts Successfully copied web-test-runner.config.mjs init succeeded."
}
},
"spellcheck": {
Expand Down

0 comments on commit c347e6d

Please sign in to comment.