File size: 1,713 Bytes
857cdcf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #!/usr/bin/env node
/**
* NPM Token Setup Script
* ตั้งค่า NPM authentication token สำหรับการ publish
*
* Usage: npm run npm:setup-token <token>
* Example: npm run npm:setup-token npm_xxxxxxxxxxxxxxxx
*/
const fs = require('fs');
const path = require('path');
const os = require('os');
const token = process.argv[2];
const npmrcPath = path.join(os.homedir(), '.npmrc');
if (!token) {
console.error(' Error: NPM token not provided');
console.error('\nUsage:');
console.error(' npm run npm:setup-token <token>');
console.error('\nExample:');
console.error(' npm run npm:setup-token npm_xxxxxxxxxxxxxxxx');
process.exit(1);
}
try {
console.log(' Setting up NPM authentication token...\n');
// อ่าน .npmrc ที่มีอยู่ (ถ้ามี)
let npmrcContent = '';
if (fs.existsSync(npmrcPath)) {
npmrcContent = fs.readFileSync(npmrcPath, 'utf-8');
}
// ลบ line เก่าที่มี _authToken
const lines = npmrcContent.split('\n').filter(line => !line.includes('_authToken'));
// เพิ่ม token ใหม่
lines.push(`//registry.npmjs.org/:_authToken=${token}`);
// บันทึก
fs.writeFileSync(npmrcPath, lines.join('\n').trim() + '\n');
console.log(' NPM token configured successfully!\n');
console.log(' Location:', npmrcPath);
console.log('\n You can now publish packages:');
console.log(' npm run npm:publish-test (dry run)');
console.log(' npm run npm:publish (actual publish)\n');
} catch (error) {
console.error(' Error setting up NPM token:', error.message);
process.exit(1);
}
|