@lawpath-tech/npm-auth 1.0.5 → 2.0.0
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/bin/cli.js +9 -18
- package/package.json +1 -1
- package/.github/workflows/publish.yml +0 -21
- package/index.js +0 -108
package/bin/cli.js
CHANGED
|
@@ -29,12 +29,6 @@ function parseNpmrc(content) {
|
|
|
29
29
|
async function main() {
|
|
30
30
|
console.log('Lawpath npm authentication setup\n');
|
|
31
31
|
|
|
32
|
-
// Skip in CI environment
|
|
33
|
-
if (process.env.CI) {
|
|
34
|
-
console.log('CI environment detected, skipping...');
|
|
35
|
-
process.exit(0);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
32
|
const npmrcPath = path.join(process.cwd(), '.npmrc');
|
|
39
33
|
|
|
40
34
|
// Read current .npmrc
|
|
@@ -76,14 +70,9 @@ async function main() {
|
|
|
76
70
|
console.log('\nFetching npm token from Lawpath config API...\n');
|
|
77
71
|
|
|
78
72
|
try {
|
|
79
|
-
|
|
80
|
-
const url = new URL(apiUrl);
|
|
81
|
-
url.searchParams.set('token', lpToken);
|
|
82
|
-
|
|
83
|
-
const response = await fetch(url.toString(), {
|
|
73
|
+
const response = await fetch(apiUrl, {
|
|
84
74
|
headers: {
|
|
85
|
-
'Authorization': `Bearer ${lpToken}
|
|
86
|
-
'Content-Type': 'application/json'
|
|
75
|
+
'Authorization': `Bearer ${lpToken}`
|
|
87
76
|
}
|
|
88
77
|
});
|
|
89
78
|
|
|
@@ -99,23 +88,25 @@ async function main() {
|
|
|
99
88
|
process.exit(1);
|
|
100
89
|
}
|
|
101
90
|
|
|
102
|
-
const {
|
|
91
|
+
const { token } = await response.json();
|
|
103
92
|
|
|
104
|
-
if (!
|
|
93
|
+
if (!token) {
|
|
105
94
|
console.error('Error: No npm token received from API');
|
|
106
95
|
process.exit(1);
|
|
107
96
|
}
|
|
108
97
|
|
|
109
98
|
// Update .npmrc with npm token
|
|
110
|
-
const authLine = `//registry.npmjs.org/:_authToken=${
|
|
99
|
+
const authLine = `//registry.npmjs.org/:_authToken=${token}`;
|
|
111
100
|
|
|
112
|
-
if (npmrc.includes('registry.npmjs.org')) {
|
|
101
|
+
if (npmrc.includes('//registry.npmjs.org/:_authToken=')) {
|
|
102
|
+
// Replace existing auth token
|
|
113
103
|
npmrc = npmrc.replace(
|
|
114
104
|
/\/\/registry\.npmjs\.org\/:_authToken=.*/,
|
|
115
105
|
authLine
|
|
116
106
|
);
|
|
117
107
|
} else {
|
|
118
|
-
|
|
108
|
+
// Add new auth token line
|
|
109
|
+
npmrc = `${authLine}\n` + npmrc.trim();
|
|
119
110
|
}
|
|
120
111
|
|
|
121
112
|
fs.writeFileSync(npmrcPath, npmrc);
|
package/package.json
CHANGED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
name: Publish to npm
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
tags:
|
|
6
|
-
- 'v*'
|
|
7
|
-
|
|
8
|
-
jobs:
|
|
9
|
-
publish:
|
|
10
|
-
runs-on: ubuntu-latest
|
|
11
|
-
steps:
|
|
12
|
-
- uses: actions/checkout@v4
|
|
13
|
-
|
|
14
|
-
- uses: actions/setup-node@v4
|
|
15
|
-
with:
|
|
16
|
-
node-version: '20'
|
|
17
|
-
registry-url: 'https://registry.npmjs.org'
|
|
18
|
-
|
|
19
|
-
- run: npm publish --access public
|
|
20
|
-
env:
|
|
21
|
-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/index.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Parse .npmrc content and extract key-value pairs
|
|
6
|
-
* @param {string} content - .npmrc file content
|
|
7
|
-
* @returns {Object} - Parsed key-value pairs
|
|
8
|
-
*/
|
|
9
|
-
function parseNpmrc(content) {
|
|
10
|
-
const config = {};
|
|
11
|
-
const lines = content.split('\n');
|
|
12
|
-
|
|
13
|
-
for (const line of lines) {
|
|
14
|
-
const trimmed = line.trim();
|
|
15
|
-
// Skip comments and empty lines
|
|
16
|
-
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
17
|
-
|
|
18
|
-
const match = trimmed.match(/^([^=]+)=(.*)$/);
|
|
19
|
-
if (match) {
|
|
20
|
-
config[match[1].trim()] = match[2].trim();
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return config;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Configure npm authentication for Lawpath private packages
|
|
29
|
-
* Reads lp_npm_token_url and lp_auth_token from .npmrc
|
|
30
|
-
* @param {Object} options
|
|
31
|
-
* @param {string} options.npmrcPath - Path to .npmrc file (defaults to cwd)
|
|
32
|
-
* @returns {Promise<void>}
|
|
33
|
-
*/
|
|
34
|
-
async function configureNpmAuth(options = {}) {
|
|
35
|
-
const {
|
|
36
|
-
npmrcPath = path.join(process.cwd(), '.npmrc')
|
|
37
|
-
} = options;
|
|
38
|
-
|
|
39
|
-
// Skip in CI environment
|
|
40
|
-
if (process.env.CI) {
|
|
41
|
-
console.log('CI environment detected, skipping npm auth setup');
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Read current .npmrc
|
|
46
|
-
let npmrc;
|
|
47
|
-
try {
|
|
48
|
-
npmrc = fs.readFileSync(npmrcPath, 'utf-8');
|
|
49
|
-
} catch (e) {
|
|
50
|
-
throw new Error(`No .npmrc found at ${npmrcPath}`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Parse .npmrc
|
|
54
|
-
const config = parseNpmrc(npmrc);
|
|
55
|
-
|
|
56
|
-
// Validate required fields
|
|
57
|
-
if (!config.lp_npm_token_url) {
|
|
58
|
-
throw new Error('Missing lp_npm_token_url in .npmrc');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (!config.lp_auth_token) {
|
|
62
|
-
throw new Error('Missing lp_auth_token in .npmrc');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const apiUrl = config.lp_npm_token_url;
|
|
66
|
-
const lpToken = config.lp_auth_token;
|
|
67
|
-
|
|
68
|
-
// Build URL with token as query param
|
|
69
|
-
const url = new URL(apiUrl);
|
|
70
|
-
url.searchParams.set('token', lpToken);
|
|
71
|
-
|
|
72
|
-
// Fetch npm token from API (token in both URL param and header)
|
|
73
|
-
const response = await fetch(url.toString(), {
|
|
74
|
-
headers: {
|
|
75
|
-
'Authorization': `Bearer ${lpToken}`,
|
|
76
|
-
'Content-Type': 'application/json'
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
if (!response.ok) {
|
|
81
|
-
if (response.status === 401) {
|
|
82
|
-
throw new Error('Invalid or expired lp_auth_token');
|
|
83
|
-
}
|
|
84
|
-
throw new Error(`API returned status ${response.status}`);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const { npmToken } = await response.json();
|
|
88
|
-
|
|
89
|
-
if (!npmToken) {
|
|
90
|
-
throw new Error('No npm token received from API');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Update .npmrc with npm token
|
|
94
|
-
const authLine = `//registry.npmjs.org/:_authToken=${npmToken}`;
|
|
95
|
-
|
|
96
|
-
if (npmrc.includes('registry.npmjs.org')) {
|
|
97
|
-
npmrc = npmrc.replace(
|
|
98
|
-
/\/\/registry\.npmjs\.org\/:_authToken=.*/,
|
|
99
|
-
authLine
|
|
100
|
-
);
|
|
101
|
-
} else {
|
|
102
|
-
npmrc = npmrc.trim() + `\n${authLine}\n`;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
fs.writeFileSync(npmrcPath, npmrc);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
module.exports = { configureNpmAuth, parseNpmrc };
|