Skip to content

Commit

Permalink
Merge pull request #2 from meddy672/feature/setup-workflows
Browse files Browse the repository at this point in the history
Feature/setup workflows
  • Loading branch information
meddy672 committed Feb 6, 2024
2 parents 3ec47d3 + d78747a commit 646e0a1
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 448 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# .github/workflows/ci.yml

name: CI

on:
push:
branches:
- 'main'
- 'development'
- 'feature**'

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Install Dependencies
run: npm install

- name: Run Tests and Linting
run: |
npm test
npm run lint
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.env
node_modules
files
files/*.docx
dist
coverage
6 changes: 3 additions & 3 deletions examples/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function main(){

filePath = await new GPTDocx({
format: BASIC,
prompt: "Write Paper on coffee.",
prompt: "Write Paper on coffee.",
}).createFile();
console.log(filePath)

Expand All @@ -43,7 +43,7 @@ async function main(){
}).createFile();
console.log(filePath)

filePath = await new GPTDocx({
filePath = await new GPTDocx({
format: UPDATE_NOTICE,
prompt: "Create an update notice for a power outage that happen in Los Angelas two weeks ago.",
}).createFile();
Expand Down Expand Up @@ -263,7 +263,7 @@ async function main(){
},
},
},
prompt: "Write a paper research paper on Jerry Rice of the San Fransico 49ers.",
prompt: "Write a research paper on Jerry Rice of the San Fransico 49ers.",
}).createFile();
console.log(filePath)

Expand Down
1 change: 1 addition & 0 deletions files/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quick fix for empty dir
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"scripts": {
"test": "nyc jest --runInBand",
"stash": "git stash save 'stash changes'",
"case": "nyc jest --runInBand ./test/GPTDocx.test.ts",
"case": "nyc jest --runInBand ./test/acceptance.test.ts",
"format": "prettier --write --config .prettierrc.json src/**/*.{js,jsx,ts,tsx}",
"lint": "eslint ./src/**/*.ts",
"clean": "rimraf dist",
Expand Down
9 changes: 4 additions & 5 deletions src/ChatGPT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
}).send();
* ```
* @async
* Class ChatGPT
*/
class ChatGPT {
/**
Expand All @@ -43,7 +42,7 @@ class ChatGPT {
*
* @param prompt **Required** a string that represents the prompt.
* @param format **Required** sent to OpenAI to provide context to the prompt.
* @param apiKeyEnv: **Optional** a string used to represent the **apikey** used in the request. Defaults to OPENAI_API_KEY.
* @param apiKeyEnv: **Optional** a string used to represent the **apikeyEnv** used in the request. Defaults to OPENAI_API_KEY.
* @param config **Optional** an object to apply additonal settings to openai.
* @returns ChatGPT instance
*/
Expand All @@ -59,7 +58,7 @@ class ChatGPT {

/**
* @description
* Takes format and builds the openai request body for the request.
* Takes the format and builds the openai request body for the request.
*
* @param {Format} format - format used for context.
* @returns ChatCompletionCreateParams
Expand Down Expand Up @@ -102,8 +101,8 @@ class ChatGPT {
console.debug("Sending resquest...");
const response: ChatCompletionResponse =
await this.openai.chat.completions.create(this.requestBody);
const content = response.choices[0].message.content as string;
return JSON.parse(content);
const context = response.choices[0].message.content as string;
return JSON.parse(context);
} catch (err) {
console.error("Unable to complete Open A.I request: ", err);
throw new Error("Error: OPENAI_REQUEST_ERROR");
Expand Down
4 changes: 2 additions & 2 deletions src/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class WordDocument {
*
* @param docName **Required** name of the document.
* @param pages **Required** an array of document components to add to the document.
* @param options **Optional** an object add addtional styling and configuration the the document.
* @param options **Optional** an object to add addtional styling and configuration the the document.
*/
constructor({ docName, pages, options }: WordDocumentArgs) {
this.options = options || DOCUMENT.BASIC;
Expand All @@ -71,7 +71,7 @@ class WordDocument {
const arrayBuffer = await blob.arrayBuffer();
const file = Buffer.from(arrayBuffer);
writeFileSync(fileName, file);
return this._name + DOCUMENT.EXT;
return fileName;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/DocxTemplater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class DocxTemplater {
private docName: string;

/**
* The word file that will used as the template.
* The word file that will be used as the template.
*/
private service: string;

Expand All @@ -46,7 +46,7 @@ export default class DocxTemplater {
* @param docName **Required** the name of the word file or docuement.
* @param service **Required** the word file that will be used as the template.
* @param response **Required** the response recieved from ChatGPT object.
* @param useAngularParser **Optional** user the angular parse or not.
* @param useAngularParser **Optional** use the angular parse or not.
* @returns
*/
constructor({
Expand Down
12 changes: 6 additions & 6 deletions src/GPTDocx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,32 +144,32 @@ class GPTDocx {
* @returns {Object} an object with requestFormat and optional styles.
*/
private _getFormat(format: string): Format {
let requestedService: any;
let requestedFormat: any;
try {
if (
process.env["NODE_ENV"] === "development" ||
process.env["NODE_ENV"] === "test"
) {
requestedService = require(
requestedFormat = require(
join(__dirname, Static.FORMATS_DIR, format, Static.INDEX_TS),
);
} else {
requestedService = require(
requestedFormat = require(
join(__dirname, Static.FORMATS_DIR, format, Static.INDEX_JS),
);
}
} catch (error) {
console.error(`Unable to find format: ${format}`);
}

return requestedService?.format;
return requestedFormat?.format;
}

/**
* @description
* Validates the format and retuns the format.
*
* Throws error if format is invalid.
* Throws parse service error if format is invalid.
*
* @private
*/
Expand Down Expand Up @@ -350,7 +350,7 @@ class GPTDocx {

/**
* @description
* Handle use case when a in a format key is **links** .
* Handle use case when a format key is **links** .
* Takes the **links** and applies the correct
* wrapper class. Adds each link to the page's
* container```this.children```. **Docx Only**.
Expand Down
3 changes: 2 additions & 1 deletion test/Document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe("Word Document", () =>{
expect(type).toEqual("string");
});
test('should trim the filename', () => {
expect(filename).toEqual("NewDocument.docx");
const { name, ext } = path.parse(filename);
expect(name+ext).toEqual("NewDocument.docx");
});
test('should call writeFileSync once with arguments', () => {
const spyArgs = writeFileSpy.mock.calls[0];
Expand Down
17 changes: 2 additions & 15 deletions test/DocxTemplater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("DocxTemplater", () => {
const filePath: any = new DocxTemplater({
docName: "A Paper About Whales",
service: "basic",
response: responseFormats["basicExample"],
response: responseFormats["basic"],
useAngularParser: false
}).create();
const { name, ext } = path.parse(filePath);
Expand All @@ -26,25 +26,12 @@ describe("DocxTemplater", () => {
const filePath: any = new DocxTemplater({
docName: "A Paper About Whales",
service: "basic",
response: responseFormats["basicExample"],
response: responseFormats["basic"],
useAngularParser: true
}).create();
const { name, ext } = path.parse(filePath);
expect(name).toEqual("APaperAboutWhales");
expect(ext).toEqual(".docx");
expect(fs.writeFileSync).toHaveBeenCalled();
});

// test("should return a filePath with a name and .docx extension when docName is undefined", () => {
// const filePath: any = new DocxTemplater({
// docName: undefined,
// service: "basicExample",
// response: responseFormats["basicExample"],
// useAngularParser: false
// }).create();
// const { name, ext } = path.parse(filePath);
// expect(name).toEqual("basicExample");
// expect(ext).toEqual(".docx");
// expect(fs.writeFileSync).toHaveBeenCalled();
// });
});
3 changes: 2 additions & 1 deletion test/GPTDocx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe("GPTDocx", () => {
let docTemplaterSpy: any;
let docxSpy: any;
beforeEach(() => {
process.env["OPENAI_API_KEY"] = "123456789";
chatGptSpy = jest.spyOn(ChatGPT.prototype, "send");
docTemplaterSpy = jest.spyOn(DocxTemplater.prototype, "create");
docxSpy = jest.spyOn(WordDocument.prototype, "saveFile");
Expand Down Expand Up @@ -379,7 +380,7 @@ describe("GPTDocx", () => {
let response: any;
try {
await new GPTDocx({
format: "basicExample",
format: "basic",
prompt: "",
}).createFile();
} catch (error: any) {
Expand Down
Loading

0 comments on commit 646e0a1

Please sign in to comment.