Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Javascript file loader #408

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Any type of contributions are welcome.

* json - [.json, .arb (Flutter Internationalization)]
* yaml - [.yaml, .yml]
* javascript - [.js]

**Feature requests and/or pull requests with new plugins are welcomed 🙂**

Expand Down
2 changes: 1 addition & 1 deletion main/pluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const saveFile = async (parsedFile: ParsedFile): Promise<boolean> => {
const updatedData = mergeDrop(data, parsedFile.data);


const serializedContent = await plugin.serialize(updatedData);
const serializedContent = await plugin.serialize(updatedData, fileContent.toString());
if (serializedContent === null) {
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion main/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import * as jsonPlugin from './json';
import * as yamlPlugin from './yaml';
import * as javascriptPlugin from './javascript';

export interface IPlugin {
fileExtensions: string[];
parse: (content: string) => Promise<any | undefined>;
serialize: (data: any) => Promise<string | undefined>;
serialize: (data: any, oldFileContent: string) => Promise<string | undefined>;
}

let plugins: IPlugin[] = [
jsonPlugin,
yamlPlugin,
javascriptPlugin,
];

export const loadPlugins = (additionalPlugins: IPlugin[]) => {
Expand Down
24 changes: 24 additions & 0 deletions main/plugins/javascript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const fileExtensions = ['.js'];

export const parse = (content: string): Promise<any> => {
try {
content = content.replace(`${getModuleExport(content)} `, '');
// replace { msg: "hello" } with { "msg": "hello" }
content = content.replace(new RegExp("(\\\"(.*?)\\\"|(\\w+))(\\s*:\\s*(\\\".*?\\\"|.))"), "\"$2$3\"$4");
return JSON.parse(content);
} catch (e) {
return Promise.resolve(undefined);
}
};

export const serialize = async (data: object, oldFileContent: string): Promise<string | undefined> => {
try {
return `${getModuleExport(oldFileContent)} ${JSON.stringify(data, null, 2)}`;
} catch (e) {
return undefined;
}
};

const getModuleExport = (content: string) => {
return content.startsWith("export default") ? "export default" : "module.exports =";
};
5 changes: 5 additions & 0 deletions testData/javascript/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
"nested": {
"content": "EN"
}
}
5 changes: 5 additions & 0 deletions testData/javascript/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
"nested": {
"content": "ES"
}
}
5 changes: 5 additions & 0 deletions testData/javascript/fr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
"nested": {
"content": "FR"
}
}