Skip to content

Commit

Permalink
Merge pull request #37 from OneSignal/misc-improvements
Browse files Browse the repository at this point in the history
Misc improvements
  • Loading branch information
rgomezp authored Jun 28, 2022
2 parents efa6a51 + ddfa40e commit 5d5f63c
Show file tree
Hide file tree
Showing 29 changed files with 905 additions and 41 deletions.
16 changes: 16 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"ignorePatterns": ["build", "src/static/*", "src/snippets/*", "src/scaffolds/*", "ts-to-es6"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"no-useless-escape": "off"
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ build
npm-debug.log
.env
.DS_Store
**/*.js
dist
ts-to-es6
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/*.js": true,
"**/*.map": true
}
}
4 changes: 2 additions & 2 deletions codegenconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"templates": [
{
"templateFile": "./main.ts"
"templateFile": "./ts-to-es6/main.js"
}
],
"compileTypeScript": true
"compileTypeScript": false
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
"description": "Used to build Web SDK libraries for various web frameworks",
"main": "index.js",
"scripts": {
"lint": "eslint . --ext .ts",
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "rm main.js && cd src && find . -type f -name '*.js' -not -path '**/node_modules/*' -delete && cd .. && echo 'Done.'",
"build": "./scripts/build.sh"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^15.3.0",
"@typescript-eslint/eslint-plugin": "^5.30.0",
"@typescript-eslint/parser": "^5.30.0",
"@yellicode/core": "^1.5.0",
"@yellicode/templating": "^1.8.0"
"@yellicode/templating": "^1.8.0",
"eslint": "^8.18.0",
"typescript": "^4.7.4"
},
"dependencies": {
"curlrequest": "^1.0.1"
Expand Down
6 changes: 6 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/bin/bash
set -euo pipefail
echo "Linting..."
yarn run lint
rm -rf build
rm -rf ts-to-es6
echo "Transpiling..."
tsc
echo "Code-generation initiating..."
yellicode
echo 'Done.'
Expand Down
5 changes: 3 additions & 2 deletions scripts/copy-static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ paths=("onesignal-ngx" "react" "vue/v2" "vue/v3")

for tgt in "${paths[@]}"; do
cp -a src/static/$tgt/* build/$tgt; # shim-specific files
rsync -vt src/static/* build/$tgt/; # shared files (e.g: issue templates)
rsync -vtr src/static/.[^.]* build/$tgt/; # shared hidden files (e.g: issue templates)
rsync -t src/static/* build/$tgt/; # shared files (e.g: issue templates)
rsync -tr src/static/$tgt/.[^.]* build/$tgt/; # shim-specific hidden files
rsync -tr src/static/.[^.]* build/$tgt/; # shared hidden files (e.g: issue templates)
done
5 changes: 3 additions & 2 deletions src/managers/CodeGenManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class CodeGenManager {
}

static async fetchOneSignalFunctions(): Promise<IFunctionSignature[]> {
let oneSignalFunctions = await FileFetchManager.getFunctions();
const oneSignalFunctions = await FileFetchManager.getFunctions();
oneSignalFunctions.unshift(INIT_FUNCTION_SIG, ON_FUNCTION_SIG, OFF_FUNCTION_SIG, ONCE_FUNCTION_SIG);
return oneSignalFunctions;
}
Expand All @@ -37,13 +37,14 @@ export class CodeGenManager {
break;
case Shim.Angular:
this.writeNgServiceFile();
break;
default:
break;
}
}

private writeIndexFile(): void {
Generator.generateAsync({outputFile: `./build/${this.shim}/${this.subdir}/index.ts`}, async (writer: TextWriter) => {
Generator.generateAsync({outputFile: `../build/${this.shim}/${this.subdir}/index.ts`}, async (writer: TextWriter) => {
let oneSignalWriter: OneSignalWriterManagerBase;
switch (this.shim) {
case Shim.React:
Expand Down
12 changes: 7 additions & 5 deletions src/managers/FileFetchManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import { IFunctionSignature } from "../models/FunctionSignature";
import { createRegex, remoteFetchFile, parseFunctionSig } from "../support/utils";

export class FileFetchManager {
static async getStubs() {
static async getStubs(): Promise<string[]> {
const stubFile = await remoteFetchFile(PATHS.ONESIGNAL_STUBS);
let regex = /FUNCTION_LIST_WITH_PROMISE_TO_STUB = \[([a-zA-Z\s,\"_]*)\]/;
return stubFile.match(regex)[1].split("\n \"").join("").split("\",").join(',').split("\"\n ").join("").split(",");
const regex = /FUNCTION_LIST_WITH_PROMISE_TO_STUB = \[([a-zA-Z\s,\"_]*)\]/;
const match = stubFile.match(regex)[1]
// removes unwanted characters and returns simple array of functions
return match.split("\n \"").join("").split("\",").join(',').split("\"\n ").join("").split(",");
}

static async getFunctions(): Promise<IFunctionSignature[]> {
const mainFile = await remoteFetchFile(PATHS.ONESIGNAL_MAIN);
let signatures = [];
const signatures = [];
(await FileFetchManager.getStubs()).forEach(stub => {
const reg = createRegex(stub);

let match = mainFile.match(reg);
const match = mainFile.match(reg);
if (match && match.length > 0) {
const sig = parseFunctionSig(match[0]);
if (FUNCTION_IGNORE.indexOf(sig.name) === -1) {
Expand Down
3 changes: 1 addition & 2 deletions src/managers/ReaderManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from 'fs';
import { FILE_WRITTEN_IN_PURE_JS } from '../support/constants';

export class ReaderManager {
static async readFile(path: string): Promise<string> {
Expand All @@ -12,7 +11,7 @@ export class ReaderManager {
console.error("Couldn't read file:", path);
return;
}
resolve(data.replace(FILE_WRITTEN_IN_PURE_JS+'\n', ''));
resolve(data);
});
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/managers/bases/OneSignalWriterManagerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { CodeWriter, TextWriter } from '@yellicode/core';
import { FUNCTION_IGNORE } from '../../support/constants';
import { IFunctionSignature } from '../../models/FunctionSignature';
import { Shim } from '../../models/Shim';
import { reactOneSignalAsyncFunctionTemplate, reactOneSignalFunctionTemplate } from '../../snippets/react/oneSignalFunctionTemplates';
import { vueOneSignalAsyncFunctionTemplate, vueOneSignalFunctionTemplate } from '../../snippets/vue/oneSignalFunctionTemplates';
import { reactOneSignalAsyncFunctionTemplate, reactOneSignalFunctionTemplate } from '../../support/react/oneSignalFunctionTemplates';
import { vueOneSignalAsyncFunctionTemplate, vueOneSignalFunctionTemplate } from '../../support/vue/oneSignalFunctionTemplates';
import { ITemplateFunctionMap } from '../../models/TemplateFunctionMap';
import { ngOneSignalAsyncFunctionTemplate, ngOneSignalFunctionTemplate } from '../../snippets/angular/oneSignalFunctionTemplates';
import { ngOneSignalAsyncFunctionTemplate, ngOneSignalFunctionTemplate } from '../../support/angular/oneSignalFunctionTemplates';

const templateFunctionMap: ITemplateFunctionMap = {
[Shim.React]: {
Expand Down
2 changes: 1 addition & 1 deletion src/managers/bases/TypingsWriterManagerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export abstract class TypingsWriterManagerBase extends CodeWriter {
* @returns void
*/
public async writeInterfaces(tabs: number): Promise<void> {
const initObjectInterfaceContents = await ReaderManager.readFile(__dirname + `/../../snippets/InitObject.ts`);
const initObjectInterfaceContents = await ReaderManager.readFile(__dirname.replace('ts-to-es6/', '') + `/../../snippets/InitObject.ts`);

const prefix = "\t".repeat(tabs);
this.writeLine(prefix+ACTION);
Expand Down
4 changes: 2 additions & 2 deletions src/managers/shims/angular/NgOneSignalWriterManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class NgOneSignalWriterManager extends OneSignalWriterManagerBase {

// implements abstract function
async writeSupportCode(): Promise<void> {
const supportFileContents = await ReaderManager.readFile(__dirname + `/../../../snippets/${Shim.Angular}/support.ts`);
const supportFileContents = await ReaderManager.readFile(__dirname.replace('ts-to-es6/', '') + `/../../../snippets/${Shim.Angular}/support.ts`);

this.writeLine(supportFileContents);
}
Expand All @@ -22,7 +22,7 @@ export class NgOneSignalWriterManager extends OneSignalWriterManagerBase {
}

async writeServiceClass(): Promise<void> {
const serviceClassFileContents = await ReaderManager.readFile(__dirname + `/../../../snippets/${Shim.Angular}/serviceClass.ts`);
const serviceClassFileContents = await ReaderManager.readFile(__dirname.replace('ts-to-es6/', '') + `/../../../snippets/${Shim.Angular}/serviceClass.ts`);
this.writeLine(serviceClassFileContents);
}
}
2 changes: 1 addition & 1 deletion src/managers/shims/angular/NgTypingsWriterManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { TypingsWriterManagerBase } from "../../bases/TypingsWriterManagerBase";
export class NgTypingsWriterManager extends TypingsWriterManagerBase {
constructor(writer: TextWriter) {
super(writer);
};
}
}
2 changes: 1 addition & 1 deletion src/managers/shims/react/ReactOneSignalWriterManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ReactOneSignalWriterManager extends OneSignalWriterManagerBase {
// implements abstract function
async writeSupportCode(): Promise<void> {
const typingsWriter = new ReactTypingsWriterManager(this);
const supportFileContents = await ReaderManager.readFile(__dirname + `/../../../snippets/${Shim.React}/support.ts`);
const supportFileContents = await ReaderManager.readFile(__dirname.replace('ts-to-es6/', '') + `/../../../snippets/${Shim.React}/support.ts`);
this.writeLine(supportFileContents);
await typingsWriter.writeInterfaces(0);
typingsWriter.writeOneSignalInterface(this.oneSignalFunctions);
Expand Down
6 changes: 3 additions & 3 deletions src/managers/shims/vue/VueOneSignalWriterManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class VueOneSignalWriterManager extends OneSignalWriterManagerBase {
// implements abstract function
async writeSupportCode(): Promise<void> {
const typingsWriter = new VueTypingsWriterManager(this);
const supportFileContents = await ReaderManager.readFile(__dirname + `/../../../snippets/${Shim.Vue}/${this.subdir}/support.ts`);
const initFileContents = await ReaderManager.readFile(__dirname + `/../../../snippets/${Shim.Vue}/onesignalInit.ts`);
const supportFileContents = await ReaderManager.readFile(__dirname.replace('ts-to-es6/', '') + `/../../../snippets/${Shim.Vue}/${this.subdir}/support.ts`);
const initFileContents = await ReaderManager.readFile(__dirname.replace('ts-to-es6/', '') + `/../../../snippets/${Shim.Vue}/onesignalInit.ts`);

this.writeLine(supportFileContents);
await typingsWriter.writeInterfaces(0);
Expand All @@ -35,7 +35,7 @@ export class VueOneSignalWriterManager extends OneSignalWriterManagerBase {
}

async writePluginCode(): Promise<void> {
const pluginFileContents = await ReaderManager.readFile(__dirname + `/../../../snippets/${Shim.Vue}/${this.subdir}/plugin.ts`);
const pluginFileContents = await ReaderManager.readFile(__dirname.replace('ts-to-es6/', '') + `/../../../snippets/${Shim.Vue}/${this.subdir}/plugin.ts`);
this.write(pluginFileContents);
}
}
2 changes: 1 addition & 1 deletion src/models/BuildSubdirectory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export enum VueVersion {
v2 = "v2",
v3 = "v3"
};
}

/**
* `BuildSubdirectory` is the type for an optional argument to the CodeGenManager constructor
Expand Down
6 changes: 4 additions & 2 deletions src/models/TemplateFunctionMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IFunctionSignature } from "./FunctionSignature";

interface ITemplateFunctionTuple {
sync: Function;
async: Function;
sync: (sig: IFunctionSignature) => string;
async: (sig: IFunctionSignature) => string;
}

export interface ITemplateFunctionMap {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// file written in pure JS
module.exports = {
env: {
browser: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// file written in pure JS
import typescript from 'rollup-plugin-typescript2';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IFunctionSignature } from "../../models/FunctionSignature";
import { spreadArgs, spreadArgsWithTypes } from "../../support/utils";
import { spreadArgs, spreadArgsWithTypes } from "../utils";

export const ngOneSignalAsyncFunctionTemplate = (sig: IFunctionSignature) => {
const args = sig.arguments?.map(arg => arg.name);
Expand Down
2 changes: 0 additions & 2 deletions src/support/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ export const GITHUB_URL = "https://raw.githubusercontent.com";

// OneSignal WebSDK functions we should not pass to `writeOneSignalFunction`
export const FUNCTION_IGNORE = ["init"];

export const FILE_WRITTEN_IN_PURE_JS = "// file written in pure JS";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IFunctionSignature } from "../../models/FunctionSignature";
import { spreadArgs, spreadArgsWithTypes } from "../../support/utils";
import { spreadArgs, spreadArgsWithTypes } from "../utils";

export const reactOneSignalAsyncFunctionTemplate = (sig: IFunctionSignature) => {
const args = sig.arguments?.map(arg => arg.name);
Expand Down
8 changes: 4 additions & 4 deletions src/support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IFunctionSignature } from '../models/FunctionSignature';

export async function remoteFetchFile(path: string): Promise<string> {
return await new Promise(resolve => {
curl.request({ url: GITHUB_URL+path }, function (err, stdout, meta) {
curl.request({ url: GITHUB_URL+path }, (err, stdout) => {
resolve(stdout);
});
})
Expand All @@ -26,8 +26,8 @@ export function createRegex(functionName: string): RegExp {
* @returns IFunctionSignature
*/
export function parseFunctionSig(signature: string): IFunctionSignature {
let functionSig= {};
let matches = signature.match(/(\S*)\s*\((.*)\)\s*:?\s*(.*)\s*{/);
const functionSig= {};
const matches = signature.match(/(\S*)\s*\((.*)\)\s*:?\s*(.*)\s*{/);

if (matches) {
functionSig['name'] = matches[1];
Expand Down Expand Up @@ -87,7 +87,7 @@ export function parseArguments(rawArgs: string): IArgument[] | void {
return;
}

let parsedArgs = [];
const parsedArgs = [];
rawArgs.split(",").forEach(arg => {
const newArg = {};
const name = arg.split(":")[0];
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"inlineSourceMap": true,
"removeComments": true,
"outDir": "./ts-to-es6"
},
"include": ["."],
"exclude": ["build", "src/scaffolds", "src/snippets", "src/static", "ts-to-es6"]
}
Loading

0 comments on commit 5d5f63c

Please sign in to comment.