@alessiofrittoli/package-configs 1.8.0-alpha.1 → 1.8.0-alpha.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alessiofrittoli/package-configs",
3
- "version": "1.8.0-alpha.1",
3
+ "version": "1.8.0-alpha.10",
4
4
  "description": "Utility library that offers base configurations for your packages.",
5
5
  "type": "module",
6
6
  "author": {
@@ -193,6 +193,6 @@
193
193
  "//4a": "*********************************************************************",
194
194
  "//4b": "******************************* OTHER *******************************",
195
195
  "//4c": "*********************************************************************",
196
- "postinstall": "node scripts/postinstall/index.ts"
196
+ "postinstall": "node scripts/postinstall/index.cjs"
197
197
  }
198
198
  }
@@ -0,0 +1,74 @@
1
+ const installDependencies = require('./install-dependencies.cjs')
2
+ const { Chain } = require('@alessiofrittoli/chain-functions')
3
+ const project = require('../../package.json')
4
+ const { readFileSync } = require('fs')
5
+ const path = require('path')
6
+
7
+ /**
8
+ * Reads and parses the `package.json` file from the specified root directory.
9
+ *
10
+ * @param {string} root The root directory where the `package.json` file is located.
11
+ *
12
+ * @throws Will throw an error if the file cannot be read or parsed.
13
+ *
14
+ * @returns {Record<string, string | Record<string, string>> & { name: string }} The parsed contents of the `package.json` file as an object.
15
+ */
16
+ const getPackageJson = root => JSON.parse(readFileSync(path.resolve(root, 'package.json')).toString())
17
+
18
+ /**
19
+ * Determines if the script is running in an external project.
20
+ *
21
+ * @param {{ root: string; name: string }} project - The options object excluding the 'outputFile' property.
22
+ * @returns {boolean} A boolean indicating whether the script is running in an external project.
23
+ * @throws Will throw an error if `INIT_CWD` is not set or if the script cannot determine the project status.
24
+ */
25
+ const isExternalPackage = ({ root, name }) => {
26
+ try {
27
+ const project = getPackageJson(root)
28
+
29
+ if (project.name.endsWith(name)) {
30
+ return false
31
+ }
32
+ return true
33
+ } catch (cause) {
34
+ throw new Error("Couldn't check if script is running in an external project.", { cause })
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Retrieves the root directory of the current process.
40
+ *
41
+ * @returns {string} The root directory of the current process, determined by the `INIT_CWD` environment variable if set,
42
+ * otherwise the current working directory.
43
+ */
44
+ const getProcessRoot = () => process.env.INIT_CWD || process.cwd()
45
+
46
+ /**
47
+ * @type import('@alessiofrittoli/chain-functions/types').LastChainLink<() => void | Promise<void>>
48
+ */
49
+ const lastChainLink = () => () => {
50
+ console.log({
51
+ package: project.name,
52
+ message: `Post-Install done. Thank you for installing ${project.name}!`,
53
+ })
54
+
55
+ process.exit(0)
56
+ }
57
+
58
+ /**
59
+ * @type import('@alessiofrittoli/chain-functions/types').ChainFactory<( params: { cwd: string; scriptsDir: string } ) => void | Promise<void>>
60
+ */
61
+ const postInstallChain = [installDependencies, lastChainLink]
62
+
63
+ const runChain = async () => {
64
+ const cwd = getProcessRoot()
65
+
66
+ if (!isExternalPackage({ name: project.name, root: cwd })) {
67
+ return process.exit(0)
68
+ }
69
+
70
+ await Chain.functions(postInstallChain)({ cwd, scriptsDir: path.resolve(__dirname, '../') })
71
+ }
72
+
73
+ // oxlint-disable-next-line typescript/no-floating-promises
74
+ runChain()
@@ -0,0 +1,196 @@
1
+ const { openSync, existsSync, readFileSync, writeFileSync } = require('fs')
2
+ const { ReadStream, WriteStream } = require('tty')
3
+ const { execSync } = require('child_process')
4
+ const prompts = require('prompts')
5
+ const path = require('path')
6
+
7
+ // // example writing user input data to stdout
8
+
9
+ // // const { openSync } = require('fs')
10
+ // // const { ReadStream, WriteStream } = require('tty')
11
+ // // const inputPath = process.platform === 'win32' ? 'CONIN$' : '/dev/tty'
12
+ // // const outputPath = process.platform === 'win32' ? 'CONOUT$' : '/dev/tty'
13
+
14
+ // // const stdin = new ReadStream(openSync(inputPath, 'r'))
15
+ // // const stdout = new WriteStream(openSync(outputPath, 'w'))
16
+ // const stdin = process.stdin
17
+ // const stdout = process.stdout
18
+
19
+ // stdin.setRawMode(true)
20
+ // stdin.resume()
21
+
22
+ // stdin.on( 'data', (data) => {
23
+ // stdout.write( data.toString() )
24
+
25
+ // if (data.includes('\x03')) {
26
+ // stdin.setRawMode(false)
27
+ // process.exit(0)
28
+ // }
29
+ // } )
30
+
31
+ /**
32
+ * Check whether script is running in a CI environment.
33
+ *
34
+ * @remarks Check if `CI` or `GITHUB_ACTIONS` is `true` or `GITHUB_WORKFLOW` is defined.
35
+ *
36
+ * @returns `true` if the script is running in a CI environment, `false` otherwise.
37
+ */
38
+ const isCIEnv = () =>
39
+ process.env.CI === 'true' ||
40
+ process.env.GITHUB_ACTIONS === 'true' ||
41
+ typeof process.env.GITHUB_WORKFLOW !== 'undefined'
42
+
43
+ /**
44
+ * Removes duplicate values from an array.
45
+ *
46
+ * @param array The input array.
47
+ * @returns The filtered array.
48
+ */
49
+ const arrayUnique = array => (array.length <= 0 ? array : [...new Set(array)])
50
+
51
+ /**
52
+ * @type import('@alessiofrittoli/chain-functions/types').ChainLink<( params: { cwd: string; scriptsDir: string } ) => void | Promise<void>>
53
+ */
54
+ const installDependencies = next => async params => {
55
+ if (isCIEnv()) {
56
+ return next(params)
57
+ }
58
+
59
+ const { cwd, scriptsDir } = params
60
+
61
+ try {
62
+ const stateFilePath = path.resolve(scriptsDir, 'post-install-data.json')
63
+
64
+ if (existsSync(stateFilePath)) {
65
+ try {
66
+ const file = JSON.parse(
67
+ readFileSync(path.resolve(scriptsDir, 'post-install-data.json'), 'utf-8'),
68
+ )
69
+ if (file.installDependencies.done) {
70
+ return next(params)
71
+ }
72
+ } catch {
73
+ return next(params)
74
+ }
75
+ }
76
+
77
+ const inputPath = process.platform === 'win32' ? 'CONIN$' : '/dev/tty'
78
+ const outputPath = process.platform === 'win32' ? 'CONOUT$' : '/dev/tty'
79
+ const stdin = !process.stdin.isTTY ? new ReadStream(openSync(inputPath, 'r')) : undefined
80
+ const stdout = !process.stdin.isTTY ? new WriteStream(openSync(outputPath, 'w')) : undefined
81
+
82
+ const { installDeps } = await prompts([
83
+ {
84
+ type: 'confirm',
85
+ name: 'installDeps',
86
+ message: 'Would you like to install optional dependencies?',
87
+ initial: true,
88
+ stdin,
89
+ stdout,
90
+ },
91
+ ])
92
+
93
+ if (!installDeps) {
94
+ return next(params)
95
+ }
96
+
97
+ const { _dependenciesChoice } = await prompts([
98
+ {
99
+ type: 'multiselect',
100
+ name: '_dependenciesChoice',
101
+ message: "Please, select which tool you're going to use",
102
+ stdin,
103
+ stdout,
104
+ choices: [
105
+ {
106
+ title: 'Vite',
107
+ selected: true,
108
+ description: 'Will install Vite',
109
+ value: ['vite'],
110
+ },
111
+ {
112
+ title: 'Vitest',
113
+ selected: true,
114
+ description: 'Will install Vitest',
115
+ value: ['vitest', '@vitest/coverage-v8'],
116
+ },
117
+ {
118
+ title: 'Oxlint',
119
+ selected: true,
120
+ description: 'Will install Oxlint',
121
+ value: ['oxlint', 'oxlint-tsgolint'],
122
+ },
123
+ {
124
+ title: 'Prettier',
125
+ selected: true,
126
+ description: 'Will install Prettier',
127
+ value: ['prettier', 'prettier-plugin-sort-imports'],
128
+ },
129
+ {
130
+ title: 'tsdown',
131
+ selected: true,
132
+ description: 'Will install tsdown',
133
+ value: ['tsdown'],
134
+ },
135
+ {
136
+ title: 'ESLint',
137
+ selected: false,
138
+ description: 'Will install ESLint dev dependencies',
139
+ value: [
140
+ 'eslint',
141
+ '@eslint/compat',
142
+ '@eslint/eslintrc',
143
+ '@eslint/js',
144
+ 'globals',
145
+ 'typescript-eslint',
146
+ ],
147
+ },
148
+ {
149
+ title: 'Next.js',
150
+ description: 'Will install Next.js related ESLint dev dependencies',
151
+ selected: false,
152
+ value: [
153
+ 'eslint',
154
+ '@eslint/compat',
155
+ '@eslint/eslintrc',
156
+ '@eslint/js',
157
+ 'globals',
158
+ 'typescript-eslint',
159
+ ],
160
+ },
161
+ ],
162
+ },
163
+ ])
164
+
165
+ if (!_dependenciesChoice) {
166
+ return next(params)
167
+ }
168
+
169
+ const dependenciesChoice = arrayUnique(_dependenciesChoice.flatMap(choice => choice))
170
+
171
+ if (dependenciesChoice.length <= 0) {
172
+ return next(params)
173
+ }
174
+
175
+ let engine = 'npm'
176
+
177
+ try {
178
+ execSync('pnpm -v', { cwd, stdio: 'ignore' })
179
+ engine = 'pnpm'
180
+ } catch {
181
+ engine = 'npm'
182
+ }
183
+
184
+ const installCommand = engine === 'npm' ? 'npm install --save-dev' : 'pnpm add --save-dev'
185
+
186
+ execSync(`${installCommand} ${dependenciesChoice.join(' ')}`, { cwd, stdio: 'ignore' })
187
+
188
+ writeFileSync(stateFilePath, JSON.stringify({ installDependencies: { done: true } }))
189
+
190
+ return next(params)
191
+ } catch {
192
+ return next(params)
193
+ }
194
+ }
195
+
196
+ module.exports = installDependencies
@@ -1,84 +0,0 @@
1
- import type { ChainFactory, LastChainLink } from '@alessiofrittoli/chain-functions/types'
2
- import { installDependencies } from './install-dependencies.ts'
3
- import project from '../../package.json' with { type: 'json' }
4
- import { Chain } from '@alessiofrittoli/chain-functions'
5
- import { readFileSync } from 'fs'
6
- import { resolve } from 'path'
7
-
8
- /**
9
- * Represents a package in the project.
10
- *
11
- */
12
- interface Package {
13
- /**
14
- * The root directory of the project.
15
- *
16
- */
17
- root: string
18
- /**
19
- * The name of the project.
20
- *
21
- */
22
- name: string
23
- }
24
-
25
- type PackageJson = Record<string, string | Record<string, string>> & { name: string }
26
-
27
- /**
28
- * Reads and parses the `package.json` file from the specified root directory.
29
- *
30
- * @param root - The root directory where the `package.json` file is located.
31
- * @returns The parsed contents of the `package.json` file as an object.
32
- * @throws Will throw an error if the file cannot be read or parsed.
33
- */
34
- const getPackageJson = (root: Package['root']): PackageJson =>
35
- JSON.parse(readFileSync(resolve(root, 'package.json')).toString()) as PackageJson
36
-
37
- /**
38
- * Determines if the script is running in an external project.
39
- *
40
- * @param project - The options object excluding the 'outputFile' property.
41
- * @returns A boolean indicating whether the script is running in an external project.
42
- * @throws Will throw an error if `INIT_CWD` is not set or if the script cannot determine the project status.
43
- */
44
- const isExternalPackage = ({ root, name }: Package): boolean => {
45
- try {
46
- const project = getPackageJson(root)
47
-
48
- if (project.name.endsWith(name)) {
49
- return false
50
- }
51
- return true
52
- } catch (cause) {
53
- throw new Error("Couldn't check if script is running in an external project.", { cause })
54
- }
55
- }
56
-
57
- /**
58
- * Retrieves the root directory of the current process.
59
- *
60
- * @returns The root directory of the current process, determined by the `INIT_CWD` environment variable if set,
61
- * otherwise the current working directory.
62
- */
63
- const getProcessRoot = (): string => process.env.INIT_CWD || process.cwd()
64
-
65
- export type PostInstallChainFn = () => void | Promise<void>
66
-
67
- const lastChainLink: LastChainLink<PostInstallChainFn> = () => () => {
68
- console.log({
69
- package: project.name,
70
- message: `Post-Install done. Thank you for installing ${project.name}!`,
71
- })
72
- }
73
-
74
- const postInstallChain: ChainFactory<PostInstallChainFn> = [installDependencies, lastChainLink]
75
-
76
- const runChain = async (): Promise<void> => {
77
- if (!isExternalPackage({ name: project.name, root: getProcessRoot() })) {
78
- return
79
- }
80
-
81
- await Chain.functions(postInstallChain)()
82
- }
83
-
84
- runChain().catch(console.error)
@@ -1,132 +0,0 @@
1
- import type { ChainLink } from '@alessiofrittoli/chain-functions/types'
2
- import type { PostInstallChainFn } from '.'
3
- import { execSync } from 'child_process'
4
- import prompts from 'prompts'
5
-
6
- /**
7
- * Check whether script is running in a CI environment.
8
- *
9
- * @remarks Check if `CI` or `GITHUB_ACTIONS` is `true` or `GITHUB_WORKFLOW` is defined.
10
- *
11
- * @returns `true` if the script is running in a CI environment, `false` otherwise.
12
- */
13
- const isCIEnv = (): boolean =>
14
- process.env.CI === 'true' ||
15
- process.env.GITHUB_ACTIONS === 'true' ||
16
- typeof process.env.GITHUB_WORKFLOW !== 'undefined'
17
-
18
- /**
19
- * Removes duplicate values from an array.
20
- *
21
- * @param array The input array.
22
- * @returns The filtered array.
23
- */
24
- export const arrayUnique = <T>(array: T[]): T[] => (array.length <= 0 ? array : [...new Set(array)])
25
-
26
- export const installDependencies: ChainLink<PostInstallChainFn> = next => async (): Promise<void> => {
27
- if (isCIEnv()) {
28
- return next()
29
- }
30
-
31
- try {
32
- const { installDeps } = await prompts([
33
- {
34
- type: 'confirm',
35
- name: 'installDeps',
36
- message: 'Would you like to install optional dependencies?',
37
- initial: true,
38
- },
39
- ])
40
-
41
- if (!installDeps) {
42
- return next()
43
- }
44
-
45
- const { _dependenciesChoice } = await prompts([
46
- {
47
- type: 'multiselect',
48
- name: '_dependenciesChoice',
49
- message: "Please, select which tool you're going to use",
50
- choices: [
51
- {
52
- title: 'Vite',
53
- selected: true,
54
- description: 'Will install Vite',
55
- value: ['vite'],
56
- },
57
- {
58
- title: 'Vitest',
59
- selected: true,
60
- description: 'Will install Vitest',
61
- value: ['vitest', '@vitest/coverage-v8'],
62
- },
63
- {
64
- title: 'Oxlint',
65
- selected: true,
66
- description: 'Will install Oxlint',
67
- value: ['oxlint', 'oxlint-tsgolint'],
68
- },
69
- {
70
- title: 'Prettier',
71
- selected: true,
72
- description: 'Will install Prettier',
73
- value: ['prettier', 'prettier-plugin-sort-imports'],
74
- },
75
- {
76
- title: 'tsdown',
77
- selected: true,
78
- description: 'Will install tsdown',
79
- value: ['tsdown'],
80
- },
81
- {
82
- title: 'ESLint',
83
- selected: false,
84
- description: 'Will install ESLint dev dependencies',
85
- value: [
86
- 'eslint',
87
- '@eslint/compat',
88
- '@eslint/eslintrc',
89
- '@eslint/js',
90
- 'globals',
91
- 'typescript-eslint',
92
- ],
93
- },
94
- {
95
- title: 'Next.js',
96
- description: 'Will install Next.js related ESLint dev dependencies',
97
- selected: false,
98
- value: [
99
- 'eslint',
100
- '@eslint/compat',
101
- '@eslint/eslintrc',
102
- '@eslint/js',
103
- 'globals',
104
- 'typescript-eslint',
105
- ],
106
- },
107
- ],
108
- },
109
- ])
110
-
111
- if (!_dependenciesChoice) {
112
- return next()
113
- }
114
-
115
- const dependenciesChoice = arrayUnique((_dependenciesChoice as string[][]).flatMap(c => c))
116
-
117
- let engine: 'npm' | 'pnpm' = 'npm'
118
-
119
- try {
120
- execSync('pnpm -v', { stdio: 'ignore' })
121
- engine = 'pnpm'
122
- } catch {
123
- engine = 'npm'
124
- }
125
-
126
- const installCommand = engine === 'npm' ? 'npm install' : 'pnpm i'
127
-
128
- execSync(`${installCommand} ${dependenciesChoice.join(' ')}`, { stdio: 'inherit' })
129
- } catch {
130
- return next()
131
- }
132
- }