Skip to content

Commit 25b02ee

Browse files
committed
refactor: remove task/tool division
- all goes into src/ - default exports - 86.74% coverage - not bad
1 parent 621b15d commit 25b02ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1583
-1287
lines changed

.fixtures/publish-cjs/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export {};
2+
//# sourceMappingURL=index.d.ts.map

.fixtures/publish-cjs/index.d.ts.map

+1
Original file line numberDiff line numberDiff line change

.fixtures/publish-cjs/index.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.fixtures/publish-cjs/index.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.fixtures/publish-cjs/lib.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type Bar = string;
2+
//# sourceMappingURL=lib.d.ts.map

.fixtures/publish-cjs/lib.d.ts.map

+1
Original file line numberDiff line numberDiff line change

.fixtures/publish-cjs/lib.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.fixtures/publish-cjs/lib.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.fixtures/publish-esm/index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './lib';
2+
export { foo } from './lib';
3+
//# sourceMappingURL=index.d.ts.map

.fixtures/publish-esm/index.d.ts.map

+1
Original file line numberDiff line numberDiff line change

.fixtures/publish-esm/index.js

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.fixtures/publish-esm/index.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.fixtures/publish-esm/lib.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export declare const foo: Bar;
2+
export type Bar = string;
3+
//# sourceMappingURL=lib.d.ts.map

.fixtures/publish-esm/lib.d.ts.map

+1
Original file line numberDiff line numberDiff line change

.fixtures/publish-esm/lib.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.fixtures/publish-esm/lib.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Error.mjs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/** This is file is part of "Ubik", (c) 2023 Hack.bg, available under GNU AGPL v3.
2+
* You should have received a copy of the GNU Affero General Public License
3+
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
4+
export default class UbikError extends Error {
5+
static required = (name) => {
6+
throw new UbikError(`Required parameter missing: ${name}`)
7+
return undefined
8+
}
9+
10+
static ModifiedPackageJSON = class ModifiedPackageJSON extends UbikError {
11+
constructor (path) {
12+
super([
13+
`This is already the modified, temporary package.json. Restore the original ` +
14+
`(e.g. "mv package.json.bak package.json" or "git checkout package.json") and try again`
15+
].join(' '))
16+
this.path = path
17+
}
18+
}
19+
20+
static TagAlreadyExists = class TagAlreadyExists extends UbikError {
21+
constructor (tag) {
22+
super([
23+
`Git tag ${tag} already exists. `,
24+
`Increment version in package.json or delete tag to proceed.`
25+
].join(' '))
26+
}
27+
}
28+
29+
static NPMErrorCode = class NPMErrorCode extends UbikError {
30+
constructor (code, name, version) {
31+
super([
32+
`ubik: NPM returned ${String(code)}`,
33+
`when looking for ${name} @ ${version}`
34+
].join(' '))
35+
}
36+
}
37+
38+
static RunFailed = class RunFailed extends UbikError {
39+
constructor (commands = []) {
40+
super('Running external commands failed. See log output for details.')
41+
this.commands = commands
42+
}
43+
}
44+
45+
static WrongMainExtension = class WrongMainExtension extends UbikError {
46+
constructor () {
47+
super([
48+
'UBIK_FORCE_TS is on, but "main" has "js" extension.',
49+
'Make "main" point to the TS index'
50+
].join(' '))
51+
}
52+
}
53+
}

tool/tool-log.mjs src/Logged.mjs

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import { resolve, dirname } from 'node:path'
55
import { fileURLToPath } from 'node:url'
66
import { readFileSync } from 'node:fs'
7+
78
import { Console, bold, Logged } from '@hackbg/logs'
89

910
const ubikPackageJson = resolve(dirname(dirname(fileURLToPath(import.meta.url))), 'package.json')
1011
const ubikVersion = JSON.parse(readFileSync(ubikPackageJson, 'utf8')).version
1112
export const console = new Console(`@hackbg/ubik ${ubikVersion}`)
12-
export { Console, bold, Logged }
13+
export { Console, bold }
14+
15+
export default Logged

src/Package.mjs

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/** This is file is part of "Ubik", (c) 2023 Hack.bg, available under GNU AGPL v3.
2+
* You should have received a copy of the GNU Affero General Public License
3+
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
4+
import Logged, { bold } from './Logged.mjs'
5+
import Error from './Error.mjs'
6+
7+
import { readFileSync } from 'node:fs'
8+
import { resolve } from 'node:path'
9+
import { execSync, execFileSync } from 'node:child_process'
10+
11+
export default class NPMPackage extends Logged {
12+
#json
13+
constructor (
14+
cwd, data = JSON.parse(readFileSync(resolve(cwd, 'package.json'), 'utf8'))
15+
) {
16+
super()
17+
this.cwd = cwd
18+
this.#json = data
19+
}
20+
get versionedName () {
21+
return `${this.name.replace('@','').replace('/','_')}_${this.version}`
22+
}
23+
get name () {
24+
return this.#json.name || ''
25+
}
26+
get version () {
27+
return this.#json.version || ''
28+
}
29+
get type () {
30+
return this.#json.type || ''
31+
}
32+
get main () {
33+
return this.#json.main || ''
34+
}
35+
set main (val) {
36+
if (!(typeof val === 'string')) {
37+
throw new Error('main must be string')
38+
}
39+
this.#json.main = val
40+
}
41+
get types () {
42+
return this.#json.types || ''
43+
}
44+
set types (val) {
45+
if (!(val instanceof Array)) {
46+
throw new Error('types must be array')
47+
}
48+
this.#json.types = val
49+
}
50+
get browser () {
51+
return this.#json.browser || ''
52+
}
53+
get exports () {
54+
return this.#json.exports || {}
55+
}
56+
set exports (val) {
57+
if (!(val instanceof Object)) {
58+
throw new Error('exports must be object')
59+
}
60+
this.#json.exports = val
61+
}
62+
get files () {
63+
return this.#json.files || []
64+
}
65+
get private () {
66+
return !!this.#json.private
67+
}
68+
get ubik () {
69+
return this.#json.ubik
70+
}
71+
set ubik (val) {
72+
this.#json.ubik = !!val
73+
}
74+
get isTypeScript () {
75+
return !!(process.env.UBIK_FORCE_TS) || this.main.endsWith('.ts')
76+
}
77+
get stringified () {
78+
return JSON.stringify(this.#json, null, 2)
79+
}
80+
}
81+
82+
/** Determine which package manager to use: */
83+
export function determinePackageManager ({
84+
packageManager = process.env.UBIK_PACKAGE_MANAGER,
85+
verbose = !!process.env.UBIK_VERBOSE,
86+
yarnCheck = 'yarn version',
87+
pnpmCheck = 'pnpm --version',
88+
} = {}) {
89+
if (packageManager) {
90+
return packageManager
91+
}
92+
93+
packageManager = 'npm'
94+
95+
try {
96+
execSync(yarnCheck)
97+
packageManager = 'yarn'
98+
} catch (e) {
99+
if (verbose) console.info('Yarn: not installed')
100+
}
101+
102+
try {
103+
execSync(pnpmCheck)
104+
packageManager = 'pnpm'
105+
} catch (e) {
106+
if (verbose) console.info('PNPM: not installed')
107+
}
108+
109+
if (verbose) console.info(
110+
`Using package manager:`, bold(packageManager),
111+
`(set`, bold('UBIK_PACKAGE_MANAGER'), 'to change)'
112+
)
113+
114+
return packageManager
115+
}
116+
117+
/** Run the selected package manager. */
118+
export function runPackageManager ({
119+
cwd = process.cwd(),
120+
npm = determinePackageManager(),
121+
args = []
122+
} = {}) {
123+
return execFileSync(npm, args, {
124+
cwd,
125+
stdio: 'inherit',
126+
env: process.env
127+
})
128+
}

src/Package.test.mjs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import assert, { equal } from 'node:assert'
2+
import { determinePackageManager } from './Package.mjs'
3+
assert(determinePackageManager())
4+
equal('foo', determinePackageManager({ verbose: true, packageManager: 'foo' }))
5+
equal('yarn', determinePackageManager({ verbose: true, yarnCheck: 'true', pnpmCheck: 'false' }))
6+
equal('pnpm', determinePackageManager({ verbose: true, yarnCheck: 'false', pnpmCheck: 'true' }))
7+
equal('npm', determinePackageManager({ verbose: true, yarnCheck: 'false', pnpmCheck: 'false' }))

0 commit comments

Comments
 (0)