Skip to content

Commit 7cd54a7

Browse files
authored
chore: update repo info, package-lock, add husky, add install/reinstall scripts (#6)
* chore: update repo info, package-lock * dev: husky eslint commit * chore: configure update/reinstall scripts, update publish workflow to execute in prod env
1 parent a424398 commit 7cd54a7

File tree

10 files changed

+4772
-7682
lines changed

10 files changed

+4772
-7682
lines changed

.github/funding.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github: [waylaidwanderer]
1+
github: [danny-avila]

.github/workflows/publish.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Node.js Package
2+
3+
# The workflow is triggered when a tag is pushed
4+
on:
5+
push:
6+
tags:
7+
- "*"
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-node@v3
15+
with:
16+
node-version: 16
17+
- run: NODE_ENV=production npm ci
18+
- run: npm run build --if-present
19+
- run: npm test
20+
21+
publish-npm:
22+
needs: build
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v3
26+
- uses: actions/setup-node@v3
27+
with:
28+
node-version: 16
29+
registry-url: 'https://registry.npmjs.org'
30+
- run: NODE_ENV=production npm ci
31+
- run: npm run build --if-present
32+
- run: npm test
33+
- run: npm publish
34+
env:
35+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

.husky/lint-staged.config.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
'*.{js,ts}': ['eslint --fix', 'eslint'],
3+
};

.husky/pre-commit

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env sh
2+
set -e
3+
. "$(dirname -- "$0")/_/husky.sh"
4+
[ -n "$CI" ] && exit 0
5+
npx lint-staged --config ./.husky/lint-staged.config.cjs

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
## Updates
66
<details open>
7+
<summary><strong>2023-10-14</strong></summary>
8+
This repo has been forked from [waylaidwanderer/node-chatgpt-api](https://github.com/waylaidwanderer/node-chatgpt-api) for active maintenance.
9+
10+
</details>
11+
12+
<details>
713
<summary><strong>2023-03-01</strong></summary>
814

915
**Support for the official ChatGPT model has been added!** You can now use the `gpt-3.5-turbo` model with the official OpenAI API, using `ChatGPTClient`. This is the same model that ChatGPT uses, and it's the most powerful model available right now. Usage of this model is **not free**, however it is **10x cheaper** (priced at $0.002 per 1k tokens) than `text-davinci-003`.

config/helpers.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Helper functions
3+
* This allows us to give the console some colour when running in a terminal
4+
*
5+
*/
6+
import fs from 'fs';
7+
import path from 'path';
8+
import readline from 'readline';
9+
import { fileURLToPath } from 'url';
10+
import { execSync } from 'child_process';
11+
12+
export const getRootDir = (trajectory = '..') => {
13+
const filename = fileURLToPath(import.meta.url);
14+
const dirname = path.dirname(filename);
15+
return path.resolve(dirname, trajectory);
16+
};
17+
18+
export const askQuestion = (query) => {
19+
const rl = readline.createInterface({
20+
input: process.stdin,
21+
output: process.stdout,
22+
});
23+
24+
return new Promise(resolve => rl.question(`\x1b[36m${query}\n> \x1b[0m`, (ans) => {
25+
rl.close();
26+
resolve(ans);
27+
}));
28+
};
29+
30+
export function isDockerRunning() {
31+
try {
32+
execSync('docker info');
33+
return true;
34+
} catch (e) {
35+
return false;
36+
}
37+
}
38+
39+
export function deleteNodeModules(dir) {
40+
const nodeModulesPath = path.join(dir, 'node_modules');
41+
if (fs.existsSync(nodeModulesPath)) {
42+
console.purple(`Deleting node_modules in ${dir}`);
43+
fs.rmSync(nodeModulesPath, { recursive: true });
44+
}
45+
}
46+
47+
export const silentExit = (code = 0) => {
48+
console.log = () => {};
49+
process.exit(code);
50+
};
51+
52+
// Set the console colours
53+
console.orange = msg => console.log('\x1b[33m%s\x1b[0m', msg);
54+
console.green = msg => console.log('\x1b[32m%s\x1b[0m', msg);
55+
console.red = msg => console.log('\x1b[31m%s\x1b[0m', msg);
56+
console.blue = msg => console.log('\x1b[34m%s\x1b[0m', msg);
57+
console.purple = msg => console.log('\x1b[35m%s\x1b[0m', msg);
58+
console.cyan = msg => console.log('\x1b[36m%s\x1b[0m', msg);
59+
console.yellow = msg => console.log('\x1b[33m%s\x1b[0m', msg);
60+
console.white = msg => console.log('\x1b[37m%s\x1b[0m', msg);
61+
console.gray = msg => console.log('\x1b[90m%s\x1b[0m', msg);

config/packages.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { execSync } from 'child_process';
2+
import path from 'path';
3+
import fs from 'fs';
4+
import { deleteNodeModules, getRootDir } from './helpers.js';
5+
6+
// Set the directories
7+
const rootDir = getRootDir();
8+
const directories = [rootDir];
9+
10+
// Delete package-lock if it exists
11+
const packageLockPath = path.resolve(rootDir, 'package-lock.json');
12+
if (fs.existsSync(packageLockPath)) {
13+
console.purple('Deleting package-lock.json...');
14+
fs.unlinkSync(packageLockPath);
15+
}
16+
17+
(async () => {
18+
// Delete all node_modules
19+
directories.forEach(deleteNodeModules);
20+
21+
// Run npm cache clean --force
22+
console.purple('Cleaning npm cache...');
23+
execSync('npm cache clean --force', { stdio: 'inherit' });
24+
25+
// Install dependencies
26+
console.purple('Installing dependencies...');
27+
execSync('npm install', { stdio: 'inherit' });
28+
})();

config/update.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { execSync } from 'child_process';
2+
import { deleteNodeModules, getRootDir } from './helpers.js';
3+
4+
const config = {
5+
skipGit: process.argv.includes('-g'),
6+
};
7+
8+
// Set the directories
9+
const rootDir = getRootDir();
10+
const directories = [rootDir];
11+
12+
(async () => {
13+
console.green(
14+
'Starting update script, this may take a minute or two depending on your system and network.',
15+
);
16+
17+
const { skipGit } = config;
18+
if (!skipGit) {
19+
// Fetch latest repo
20+
console.purple('Fetching the latest repo...');
21+
execSync('git fetch origin', { stdio: 'inherit' });
22+
23+
// Switch to main branch
24+
console.purple('Switching to main branch...');
25+
execSync('git checkout main', { stdio: 'inherit' });
26+
27+
// Git pull origin main
28+
console.purple('Pulling the latest code from main...');
29+
execSync('git pull origin main', { stdio: 'inherit' });
30+
}
31+
32+
// Delete all node_modules
33+
directories.forEach(deleteNodeModules);
34+
35+
// Run npm cache clean --force
36+
console.purple('Cleaning npm cache...');
37+
execSync('npm cache clean --force', { stdio: 'inherit' });
38+
39+
// Install dependencies
40+
console.purple('Installing dependencies...');
41+
execSync('npm ci', { stdio: 'inherit' });
42+
43+
console.green('node-gpt-api is now up to date!');
44+
})();

0 commit comments

Comments
 (0)