virmator 4.2.18 → 4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -121,6 +121,14 @@ Initialize a repo with all virmator config files.
121
121
 
122
122
  force overwrite files even if they already exist.
123
123
 
124
+ ## publish
125
+
126
+ Publish an npm package. Includes workspace support.
127
+
128
+ ### Examples
129
+
130
+ - publish a package: `virmator publish`
131
+
124
132
  ## spellcheck
125
133
 
126
134
  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.
@@ -57,6 +57,9 @@ const baseConfig = {
57
57
  'peta',
58
58
  'postpack',
59
59
  'postupgrade',
60
+ 'premajor',
61
+ 'preminor',
62
+ 'prepatch',
60
63
  'prettierignore',
61
64
  'prettierrc',
62
65
  'preupgrade',
@@ -75,6 +78,7 @@ const baseConfig = {
75
78
  'threejs',
76
79
  'transpiled',
77
80
  'transpiling',
81
+ 'tsbuildinfo',
78
82
  'typeof',
79
83
  'virmator',
80
84
  'vite',
@@ -1,6 +1,7 @@
1
1
  .*
2
2
  *.test-helper.*
3
3
  *.test.*
4
+ *.tsbuildinfo
4
5
  **/*/readme-examples/
5
6
  **/*/test/
6
7
  /*.tgz
@@ -30,6 +30,7 @@ const fs_1 = require("fs");
30
30
  const promises_1 = require("fs/promises");
31
31
  const path_1 = require("path");
32
32
  const semver = __importStar(require("semver"));
33
+ const fs_2 = require("../../augments/fs");
33
34
  const cli_color_1 = require("../cli-color");
34
35
  async function updateDepsAsNeeded(inputs) {
35
36
  try {
@@ -92,7 +93,7 @@ async function getVersionsToUpgradeTo({ repoDir, packageDir, npmDeps, packageBin
92
93
  });
93
94
  }
94
95
  async function findDepsListWithPackageName(repoDir, packageName) {
95
- if (repoDir === (0, path_1.parse)(process.cwd()).root) {
96
+ if (repoDir === fs_2.systemRootPath) {
96
97
  return {};
97
98
  }
98
99
  if (!(0, fs_1.existsSync)((0, path_1.join)(repoDir, 'package.json'))) {
@@ -0,0 +1,9 @@
1
+ export declare function askQuestion(questionToAsk: string, timeoutMs?: number): Promise<string>;
2
+ export declare function askQuestionUntilConditionMet({ questionToAsk, conditionCallback, invalidInputMessage, tryCountMax, timeoutMs, }: {
3
+ questionToAsk: string;
4
+ conditionCallback: (response: string) => boolean | Promise<boolean>;
5
+ invalidInputMessage: string;
6
+ tryCountMax?: number;
7
+ /** Set to 0 to disable the timeout */
8
+ timeoutMs?: number;
9
+ }): Promise<string>;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.askQuestionUntilConditionMet = exports.askQuestion = void 0;
4
+ const readline_1 = require("readline");
5
+ async function askQuestion(questionToAsk, timeoutMs = 60000) {
6
+ const cliInterface = (0, readline_1.createInterface)({
7
+ input: process.stdin,
8
+ output: process.stdout,
9
+ });
10
+ return new Promise((resolve, reject) => {
11
+ const timeoutId = timeoutMs
12
+ ? setTimeout(() => {
13
+ cliInterface.close();
14
+ reject(`Took too long to response (over "${timeoutMs / 1000}" seconds)`);
15
+ }, timeoutMs)
16
+ : undefined;
17
+ cliInterface.question(questionToAsk + ' ', (response) => {
18
+ if (timeoutId != undefined) {
19
+ clearTimeout(timeoutId);
20
+ }
21
+ cliInterface.close();
22
+ resolve(response);
23
+ });
24
+ });
25
+ }
26
+ exports.askQuestion = askQuestion;
27
+ async function askQuestionUntilConditionMet({ questionToAsk, conditionCallback, invalidInputMessage, tryCountMax = 5, timeoutMs = 60000, }) {
28
+ let wasConditionMet = false;
29
+ let retryCount = 0;
30
+ let response = '';
31
+ while (!wasConditionMet && retryCount <= tryCountMax) {
32
+ response = (await askQuestion(questionToAsk, timeoutMs)).trim();
33
+ wasConditionMet = await conditionCallback(response);
34
+ if (!wasConditionMet) {
35
+ console.error(invalidInputMessage);
36
+ }
37
+ retryCount++;
38
+ }
39
+ if (retryCount > tryCountMax) {
40
+ throw new Error(`Max input attempts (${tryCountMax}) exceeded.`);
41
+ }
42
+ return response;
43
+ }
44
+ exports.askQuestionUntilConditionMet = askQuestionUntilConditionMet;
@@ -1,6 +1,7 @@
1
1
  export interface DirContents {
2
2
  [key: string]: string | DirContents;
3
3
  }
4
+ export declare const systemRootPath: string;
4
5
  export declare function readAllDirContents({ dir, recursive, }: {
5
6
  dir: string;
6
7
  recursive?: boolean;
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.recursivelyUpwardsSearchForDir = exports.writeFiles = exports.readAllDirContents = void 0;
3
+ exports.recursivelyUpwardsSearchForDir = exports.writeFiles = exports.readAllDirContents = exports.systemRootPath = void 0;
4
4
  const augment_vir_1 = require("augment-vir");
5
5
  const fs_1 = require("fs");
6
6
  const promises_1 = require("fs/promises");
7
7
  const path_1 = require("path");
8
+ exports.systemRootPath = (0, path_1.parse)(process.cwd()).root;
8
9
  async function readAllDirContents({ dir, recursive = false, }) {
9
10
  const fileNames = await (0, promises_1.readdir)(dir);
10
11
  const allFileContents = await Promise.all(fileNames.map(async (fileName) => {
@@ -0,0 +1 @@
1
+ export declare function doChangesExist(repoDirPath: string): Promise<boolean>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.doChangesExist = void 0;
4
+ const node_only_1 = require("augment-vir/dist/cjs/node-only");
5
+ async function doChangesExist(repoDirPath) {
6
+ const getChangesCommand = `git status --porcelain=v1 2>/dev/null`;
7
+ const getChangesOutput = await (0, node_only_1.runShellCommand)(getChangesCommand, {
8
+ cwd: repoDirPath,
9
+ rejectOnError: true,
10
+ });
11
+ return !!getChangesOutput.stdout;
12
+ }
13
+ exports.doChangesExist = doChangesExist;
@@ -0,0 +1,11 @@
1
+ import { RequiredAndNotNullBy } from 'augment-vir';
2
+ import { PackageJson } from 'type-fest';
3
+ export declare type CleanPackageJson = Readonly<RequiredAndNotNullBy<PackageJson.PackageJsonStandard, 'name' | 'version'> & {
4
+ workspaces?: ReadonlyArray<string>;
5
+ }>;
6
+ export declare function findTopLevelPackageJsonFile(startDir: string): string | undefined;
7
+ export declare function readPackageJson(packageJsonPath: string): Promise<CleanPackageJson>;
8
+ export declare function readTopLevelPackageJson(startDir: string): Promise<Readonly<{
9
+ packageJson: CleanPackageJson;
10
+ path: string;
11
+ }>>;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readTopLevelPackageJson = exports.readPackageJson = exports.findTopLevelPackageJsonFile = void 0;
4
+ const augment_vir_1 = require("augment-vir");
5
+ const fs_1 = require("fs");
6
+ const promises_1 = require("fs/promises");
7
+ const path_1 = require("path");
8
+ const fs_2 = require("./fs");
9
+ function findTopLevelPackageJsonFile(startDir) {
10
+ if (startDir === fs_2.systemRootPath) {
11
+ return undefined;
12
+ }
13
+ const topLevelPath = findTopLevelPackageJsonFile((0, path_1.dirname)(startDir));
14
+ const currentLevelPath = (0, path_1.join)(startDir, 'package.json');
15
+ if (topLevelPath) {
16
+ return topLevelPath;
17
+ }
18
+ else if ((0, fs_1.existsSync)(currentLevelPath)) {
19
+ return currentLevelPath;
20
+ }
21
+ else {
22
+ return undefined;
23
+ }
24
+ }
25
+ exports.findTopLevelPackageJsonFile = findTopLevelPackageJsonFile;
26
+ async function readPackageJson(packageJsonPath) {
27
+ let parsed;
28
+ try {
29
+ parsed = JSON.parse((await (0, promises_1.readFile)(packageJsonPath)).toString());
30
+ }
31
+ catch (error) {
32
+ throw new Error(`Failed to read package json file "${packageJsonPath}": ${(0, augment_vir_1.extractErrorMessage)(error)}`);
33
+ }
34
+ if (!parsed.name) {
35
+ throw new Error(`Missing package name from "${packageJsonPath}"`);
36
+ }
37
+ if (!parsed.version) {
38
+ throw new Error(`Missing package version from "${packageJsonPath}"`);
39
+ }
40
+ return parsed;
41
+ }
42
+ exports.readPackageJson = readPackageJson;
43
+ async function readTopLevelPackageJson(startDir) {
44
+ const topLevelPackageJsonPath = findTopLevelPackageJsonFile(startDir);
45
+ if (!topLevelPackageJsonPath) {
46
+ throw new Error(`Failed to find package.json path in any ancestor directories from "${startDir}"`);
47
+ }
48
+ return {
49
+ packageJson: await readPackageJson(topLevelPackageJsonPath),
50
+ path: topLevelPackageJsonPath,
51
+ };
52
+ }
53
+ exports.readTopLevelPackageJson = readTopLevelPackageJson;
@@ -0,0 +1,6 @@
1
+ export declare const publishCommandDefinition: import("../api/command/define-command").CommandDefinition<{
2
+ commandName: "publish";
3
+ subCommandDescriptions: {};
4
+ configFiles: {};
5
+ npmDeps: readonly [];
6
+ }>;
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.publishCommandDefinition = void 0;
7
+ const augment_vir_1 = require("augment-vir");
8
+ const node_only_1 = require("augment-vir/dist/cjs/node-only");
9
+ const fs_1 = require("fs");
10
+ const promises_1 = require("fs/promises");
11
+ const glob_1 = __importDefault(require("glob"));
12
+ const path_1 = require("path");
13
+ const semver_1 = require("semver");
14
+ const define_command_1 = require("../api/command/define-command");
15
+ const console_1 = require("../augments/console");
16
+ const git_1 = require("../augments/git");
17
+ const npm_1 = require("../augments/npm");
18
+ exports.publishCommandDefinition = (0, define_command_1.defineCommand)({
19
+ commandName: 'publish',
20
+ subCommandDescriptions: {},
21
+ configFiles: {},
22
+ npmDeps: [],
23
+ }, ({ commandName, packageBinName }) => {
24
+ return {
25
+ sections: [
26
+ {
27
+ title: '',
28
+ content: `Publish an npm package. Includes workspace support.`,
29
+ },
30
+ ],
31
+ examples: [
32
+ {
33
+ title: 'publish a package',
34
+ content: `${packageBinName} ${commandName}`,
35
+ },
36
+ ],
37
+ };
38
+ }, async (inputs) => {
39
+ const { packageJson, path: packageJsonPath } = await (0, npm_1.readTopLevelPackageJson)(inputs.repoDir);
40
+ if (await (0, git_1.doChangesExist)((0, path_1.dirname)(packageJsonPath))) {
41
+ throw new Error(`git changes detected in "${packageJsonPath}". Cannot run publish command with changes, commit them first.`);
42
+ }
43
+ const workspaceDirs = getWorkspaceDirs(packageJson, packageJsonPath);
44
+ if (await isCurrentVersionPublished(packageJson, workspaceDirs)) {
45
+ await bumpPackageVersion(packageJson, packageJsonPath, workspaceDirs);
46
+ }
47
+ await updateWorkspaceVersions(packageJsonPath, workspaceDirs);
48
+ await publishPackages(workspaceDirs, packageJsonPath);
49
+ await updateGit(packageJsonPath);
50
+ return {
51
+ success: true,
52
+ };
53
+ });
54
+ async function updateGit(packageJsonPath) {
55
+ const newVersion = (await (0, npm_1.readPackageJson)(packageJsonPath)).version;
56
+ const packageDirPath = (0, path_1.dirname)(packageJsonPath);
57
+ if (await (0, git_1.doChangesExist)(packageDirPath)) {
58
+ await (0, node_only_1.runShellCommand)(`git commit -a --amend --no-edit`, {
59
+ cwd: packageDirPath,
60
+ rejectOnError: true,
61
+ });
62
+ }
63
+ await (0, node_only_1.runShellCommand)(`git tag v${newVersion} && git push && git push --tags`, {
64
+ cwd: packageDirPath,
65
+ rejectOnError: true,
66
+ });
67
+ }
68
+ async function updateWorkspaceVersions(packageJsonPath, workspaceDirPaths) {
69
+ const newVersion = (await (0, npm_1.readPackageJson)(packageJsonPath)).version;
70
+ await (0, augment_vir_1.awaitedForEach)(workspaceDirPaths, async (workspacePath) => {
71
+ const workspacePackageJsonPath = (0, path_1.join)(workspacePath, 'package.json');
72
+ const workspacePackageJson = await (0, npm_1.readPackageJson)(workspacePackageJsonPath);
73
+ const newWorkspacePackageJson = { ...workspacePackageJson };
74
+ newWorkspacePackageJson.version = newVersion;
75
+ await (0, promises_1.writeFile)(workspacePackageJsonPath, JSON.stringify(newWorkspacePackageJson, null, 4) + '\n');
76
+ });
77
+ }
78
+ async function publishPackages(workspaceDirPaths, packageJsonPath) {
79
+ const packagePath = (0, path_1.dirname)(packageJsonPath);
80
+ if (workspaceDirPaths.length) {
81
+ await (0, augment_vir_1.awaitedForEach)(workspaceDirPaths, async (workspaceDirPath) => {
82
+ const workspacePackageJson = await (0, npm_1.readPackageJson)((0, path_1.join)(workspaceDirPath, 'package.json'));
83
+ if (!workspacePackageJson.private) {
84
+ console.info(`\nPublishing ${(0, path_1.basename)(workspaceDirPath)}`);
85
+ await (0, node_only_1.runShellCommand)(`npm publish`, {
86
+ rejectOnError: true,
87
+ cwd: workspaceDirPath,
88
+ hookUpToConsole: true,
89
+ env: {
90
+ ...process.env,
91
+ NPM_CONFIG_COLOR: 'always',
92
+ },
93
+ });
94
+ }
95
+ });
96
+ }
97
+ else {
98
+ await (0, node_only_1.runShellCommand)(`npm publish`, {
99
+ rejectOnError: true,
100
+ cwd: packagePath,
101
+ hookUpToConsole: true,
102
+ env: {
103
+ ...process.env,
104
+ NPM_CONFIG_COLOR: 'always',
105
+ },
106
+ });
107
+ }
108
+ }
109
+ async function isCurrentVersionPublished(packageJson, workspaceDirs) {
110
+ const arePublished = await Promise.all(workspaceDirs.map(async (workspaceDirPath) => {
111
+ const workspacePackageJson = await (0, npm_1.readPackageJson)((0, path_1.join)(workspaceDirPath, 'package.json'));
112
+ return await isPublished(workspacePackageJson);
113
+ }));
114
+ return arePublished.some((entry) => entry) || (await isPublished(packageJson));
115
+ }
116
+ async function isPublished({ name, version }) {
117
+ const output = await (0, node_only_1.runShellCommand)(`npm show ${name}@${version}`);
118
+ return output.exitCode === 0;
119
+ }
120
+ // this is just used to ensure we have every release type
121
+ const releaseTypesObject = {
122
+ major: 'major',
123
+ premajor: 'premajor',
124
+ minor: 'minor',
125
+ preminor: 'preminor',
126
+ patch: 'patch',
127
+ prepatch: 'prepatch',
128
+ prerelease: 'prerelease',
129
+ };
130
+ const releaseTypeOptions = (0, augment_vir_1.getObjectTypedKeys)(releaseTypesObject);
131
+ const releaseTypeMessage = `Choose a release type: ${(0, augment_vir_1.joinWithFinalConjunction)(releaseTypeOptions, 'or')}:`;
132
+ async function bumpPackageVersion(packageJson, packageJsonPath, workspaceDirPaths) {
133
+ const releaseType = (await (0, console_1.askQuestionUntilConditionMet)({
134
+ conditionCallback: (input) => {
135
+ return releaseTypeOptions.includes(input.toLowerCase());
136
+ },
137
+ invalidInputMessage: `Invalid release type. Expected ${(0, augment_vir_1.joinWithFinalConjunction)(releaseTypeOptions, 'or')}.`,
138
+ questionToAsk: releaseTypeMessage,
139
+ timeoutMs: 0,
140
+ }));
141
+ const newVersion = (0, semver_1.inc)(packageJson.version, releaseType);
142
+ if (!newVersion) {
143
+ throw new Error(`New version was not calculated from version "${packageJson.version}" and release type "${releaseType}"`);
144
+ }
145
+ const newPackageJson = {
146
+ ...packageJson,
147
+ };
148
+ // assign this after the const assignment above so we don't change the key ordering
149
+ newPackageJson.version = newVersion;
150
+ await (0, promises_1.writeFile)(packageJsonPath, JSON.stringify(newPackageJson, null, 4) + '\n');
151
+ }
152
+ function getWorkspaceDirs(packageJson, packageJsonPath) {
153
+ if (!packageJson.workspaces) {
154
+ return [];
155
+ }
156
+ const paths = packageJson.workspaces
157
+ .map((workspacePattern) => {
158
+ const packageDir = (0, path_1.dirname)(packageJsonPath);
159
+ const exactPath = (0, path_1.join)(packageDir, workspacePattern);
160
+ if ((0, fs_1.existsSync)(exactPath)) {
161
+ return exactPath;
162
+ }
163
+ else {
164
+ return glob_1.default.sync(workspacePattern, {
165
+ cwd: packageDir,
166
+ });
167
+ }
168
+ })
169
+ .flat()
170
+ .sort();
171
+ return paths;
172
+ }
package/dist/index.d.ts CHANGED
@@ -71,6 +71,11 @@ export declare const virmator: Readonly<{
71
71
  };
72
72
  configFiles: {};
73
73
  npmDeps: readonly ["prettier", "prettier-plugin-jsdoc", "prettier-plugin-multiline-arrays", "prettier-plugin-organize-imports", "prettier-plugin-packagejson", "prettier-plugin-sort-json", "prettier-plugin-toml"];
74
+ }> | import("./api/command/define-command").CommandDefinition<{
75
+ commandName: "publish";
76
+ subCommandDescriptions: {};
77
+ configFiles: {};
78
+ npmDeps: readonly [];
74
79
  }> | import("./api/command/define-command").CommandDefinition<{
75
80
  commandName: "spellcheck";
76
81
  subCommandDescriptions: {};
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ const compile_command_1 = require("./commands/compile.command");
21
21
  const format_command_1 = require("./commands/format.command");
22
22
  const frontend_command_1 = require("./commands/frontend.command");
23
23
  const init_command_1 = require("./commands/init.command");
24
+ const publish_command_1 = require("./commands/publish.command");
24
25
  const spellcheck_command_1 = require("./commands/spellcheck.command");
25
26
  const test_web_command_1 = require("./commands/test-web.command");
26
27
  const test_command_1 = require("./commands/test.command");
@@ -49,9 +50,10 @@ exports.virmator = (0, create_virmator_1.createVirmator)({
49
50
  format_command_1.formatCommandDefinition,
50
51
  frontend_command_1.frontendCommandDefinition,
51
52
  init_command_1.initCommandDefinition,
53
+ publish_command_1.publishCommandDefinition,
52
54
  spellcheck_command_1.spellcheckCommandDefinition,
53
- test_web_command_1.testWebCommandDefinition,
54
55
  test_command_1.testCommandDefinition,
56
+ test_web_command_1.testWebCommandDefinition,
55
57
  update_configs_command_1.updateConfigsCommandDefinition,
56
58
  ],
57
59
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "virmator",
3
- "version": "4.2.18",
3
+ "version": "4.3.1",
4
4
  "description": "Handle all the stuffs in one package.",
5
5
  "keywords": [
6
6
  "automation",
@@ -28,14 +28,14 @@
28
28
  "bin": "dist/cli.js",
29
29
  "scripts": {
30
30
  "compile": "rm -rf dist && tsc",
31
+ "docs:check": "npm run compile && ts-node -r tsconfig-paths/register src/scripts/save-help-message.ts check && md-code --check README.md",
32
+ "docs:update": "npm run compile && ts-node -r tsconfig-paths/register src/scripts/save-help-message.ts && md-code README.md",
31
33
  "format": "npm run format:base -- --write --list-different",
32
34
  "format:base": "prettier \"./**/*.+(ts|json|html|css|md|js)\"",
33
35
  "prepublishOnly": "npm run test:all",
34
- "readme:check": "npm run compile && ts-node -r tsconfig-paths/register src/scripts/save-help-message.ts check && md-code --check README.md",
35
- "readme:update": "npm run compile && ts-node -r tsconfig-paths/register src/scripts/save-help-message.ts && md-code README.md",
36
36
  "spellcheck": "cspell \"{*,.*,**/{.*,*}/**/{.*,*}}\" --exclude \"test-files/spellcheck/invalid-spellcheck-repo/**/*\" --exclude \"extender-configs\"",
37
37
  "test": "bash src/scripts/run-tests.sh",
38
- "test:all": "npm run test && npm run spellcheck -- --no-progress && npm run test:format && npm run readme:check",
38
+ "test:all": "npm run test && npm run spellcheck -- --no-progress && npm run test:format && npm run docs:check",
39
39
  "test:format": "npm run format:base -- --check",
40
40
  "test:help": "ts-node -r tsconfig-paths/register src/cli/cli.ts --help",
41
41
  "test:types": "tsc --noEmit"
@@ -43,6 +43,7 @@
43
43
  "dependencies": {
44
44
  "ansi-colors": "^4.1.3",
45
45
  "augment-vir": "^3.1.2",
46
+ "glob": "^8.0.3",
46
47
  "semver": "^7.3.8",
47
48
  "simple-git": "^3.15.0"
48
49
  },
@@ -51,6 +52,7 @@
51
52
  "@open-wc/testing": "^3.1.7",
52
53
  "@types/chai": "^4.3.4",
53
54
  "@types/fs-extra": "^9.0.13",
55
+ "@types/glob": "^8.0.0",
54
56
  "@types/mocha": "^10.0.0",
55
57
  "@types/node": "^18.11.9",
56
58
  "@types/semver": "^7.3.13",
@@ -77,6 +79,7 @@
77
79
  "prettier-plugin-toml": "^0.3.1",
78
80
  "ts-node": "^10.9.1",
79
81
  "tsconfig-paths": "^4.1.0",
82
+ "type-fest": "^3.2.0",
80
83
  "typescript": "^4.8.4",
81
84
  "vite": "^3.2.3"
82
85
  },