Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(*): add helper localization function #10

Merged
merged 1 commit into from
Oct 9, 2018
Merged
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
20 changes: 9 additions & 11 deletions components/convert-component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as process from 'process';

// import * as process from 'process';
import { Component, ConverterComponent } from 'typedoc/dist/lib/converter/components';
import { Converter } from 'typedoc/dist/lib/converter';
import { ReflectionKind } from 'typedoc/dist/lib/models';
Expand All @@ -11,7 +10,7 @@ import { Parser } from '../utils/parser';
import { Constants } from '../utils/constants';
import { InterfaceFactory } from '../utils/factories/interface-factory';
import { FunctionFactory } from '../utils/factories/function-factory';

@Component({ name: 'convert-component' })
export class ConvertComponent extends ConverterComponent {
jsonObjectName: string;
Expand All @@ -28,26 +27,25 @@ export class ConvertComponent extends ConverterComponent {
[Converter.EVENT_RESOLVE]: this.resolve,
[Converter.EVENT_RESOLVE_BEGIN]: this.onResolveBegin,
[Converter.EVENT_RESOLVE_END]: this.onResolveEnd,
[Converter.EVENT_END]: this.onEnd,
[Converter.EVENT_BEGIN]: this.onBegin
});

this.parser = new Parser();
this.fileOperations = new FileOperations(this.application.logger);
}

private onBegin(...rest) {
private onBegin() {
const options = this.application.options.getRawValues();
this.mainDirToExport = options[Constants.CONVERT_COMMAND];
this.mainDirToExport = options[Constants.CONVERT_OPTION];

if(!this.fileOperations.ifDirectoryExists(this.mainDirToExport)) {
this.fileOperations.createDir(this.mainDirToExport);
}
}

private onEnd(...rest) {
process.exit(0);
}
// private onEnd(...rest) {
// // process.exit(0);
// }

private onResolveBegin(context) {
const files = context.project.files;
Expand Down Expand Up @@ -130,11 +128,11 @@ export class ConvertComponent extends ConverterComponent {

let comment = this.getCommentData(reflection.comment);

if (options[Constants.INCLUDE_TAGS_COMMAND] && reflection.comment.tags) {
if (options[Constants.INCLUDE_TAGS_OPTION] && reflection.comment.tags) {
comment[Constants.COMMENT] = Object.assign(this.getTagsComments(reflection.comment), comment[Constants.COMMENT]);
}

if (options[Constants.INCLUDE_PARAMS_COMMAND] && reflection.parameters) {
if (options[Constants.INCLUDE_PARAMS_OPTION] && reflection.parameters) {
comment[Constants.COMMENT] = Object.assign(this.getParamsComments(reflection), comment[Constants.COMMENT]);
}

Expand Down
37 changes: 25 additions & 12 deletions components/options-component.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,54 @@
import { Component } from 'typedoc/dist/lib/output/components'
import { OptionsComponent, DiscoverEvent } from 'typedoc/dist/lib/utils/options/options'
import { OptionDeclaration, DeclarationOption, ParameterType, ParameterHint } from 'typedoc/dist/lib/utils/options/declaration';
import { OptionDeclaration, DeclarationOption, ParameterType } from 'typedoc/dist/lib/utils/options/declaration';
import { Option } from 'typedoc/dist/lib/utils/component';
import { Constants } from '../utils/constants';

@Component({ name: 'options-component' })
export class OptComponent extends OptionsComponent {
public initialize() {
public initialize() {
const generateToOption: DeclarationOption = {
name: Constants.CONVERT_COMMAND,
short: Constants.SHORT_CONVERT_COMMAND,
name: Constants.CONVERT_OPTION,
short: Constants.SHORT_CONVERT_OPTION,
help: 'Specifies the directory where the json files have to be generated.',
};

const generateFromOption: DeclarationOption = {
name: Constants.RENDER_COMMAND,
short: Constants.SHORT_RENDER_COMMAND,
name: Constants.RENDER_OPTION,
short: Constants.SHORT_RENDER_OPTION,
help: 'Specify from where to get the loclized json data.'
}

const includeTagsOption: DeclarationOption = {
name: Constants.INCLUDE_TAGS_COMMAND,
name: Constants.INCLUDE_TAGS_OPTION,
help: 'Specify whether to include tags per comment.',
type: ParameterType.Boolean,
defaultValue: true
}

const includeParamsOption: DeclarationOption = {
name: Constants.INCLUDE_PARAMS_COMMAND,
name: Constants.INCLUDE_PARAMS_OPTION,
help: 'Specify whether to include params per comment.',
type: ParameterType.Boolean,
defaultValue: true
}

this.application.options.addDeclaration(new OptionDeclaration(generateToOption));
this.application.options.addDeclaration(new OptionDeclaration(generateFromOption));
this.application.options.addDeclaration(new OptionDeclaration(includeTagsOption));
this.application.options.addDeclaration(new OptionDeclaration(includeParamsOption));
const localizeOption: DeclarationOption = {
name: Constants.LOCALIZE_OPTION,
help: 'Specify your localization for instance (jp)'
}

const shellStringsOption: DeclarationOption = {
name: Constants.TEMPLATE_STRINGS_OPTION,
help: 'Path to the json file which contains your localized template strings'
}

this.application.options.addDeclarations([
generateToOption,
generateFromOption,
includeTagsOption,
includeParamsOption,
localizeOption,
shellStringsOption]);
}
}
32 changes: 26 additions & 6 deletions components/render-component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Component, RendererComponent } from 'typedoc/dist/lib/output/components';
import * as path from 'path';
import { name as projName} from '../package.json';

import { Component, RendererComponent } from 'typedoc/dist/lib/output/components';
import { ReflectionKind } from 'typedoc/dist/lib/models';
import { FileOperations } from '../utils/file-operations';
import { AttributeType } from '../utils/enums/json-obj-kind';
import { AttributeType } from '../utils/enums/json-keys';
import { Constants } from '../utils/constants';
import { RendererEvent } from 'typedoc/dist/lib/output/events';
import { Parser } from '../utils/parser';
Expand All @@ -17,18 +19,19 @@ export class RenderComponenet extends RendererComponent {

public initialize() {
this.listenTo(this.owner, {
[RendererEvent.BEGIN]: this.onRenderBegin,
[RendererEvent.BEGIN]: this.onRenderBegin
});

this.fileOperations = new FileOperations(this.application.logger);
this.parser = new Parser();
}

private onRenderBegin(event: RendererEvent) {

private onRenderBegin(event) {
this.registerHelpers();

const reflections = event.project.reflections;
const options = this.application.options.getRawValues();
const localizeOpt = options[Constants.RENDER_COMMAND];
const localizeOpt = options[Constants.RENDER_OPTION];
if (localizeOpt) {
this.mainDirOfJsons = localizeOpt;
this.globalFuncsData = this.fileOperations.getFileData(this.mainDirOfJsons, Constants.GLOBAL_FUNCS_FILE_NAME, 'json');
Expand Down Expand Up @@ -144,4 +147,21 @@ export class RenderComponenet extends RendererComponent {

return reflection.parent;
}

private registerHelpers() {
let module;
try {
module = require.resolve(projName);
} catch(e) {
this.application.logger.error(e.message);
return;
}

const pluginDist = path.dirname(require.resolve(module));
if (pluginDist) {
this.owner.theme.resources.deactivate();
this.owner.theme.resources.helpers.addOrigin('custom-helpers', `${pluginDist}\\utils\\helpers`);
this.owner.theme.resources.activate();
}
}
}
18 changes: 9 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as process from 'process';

import { Application } from 'typedoc/dist/lib/application'
import { ConvertComponent } from './components/convert-component';
import { RenderComponenet } from './components/render-component';
import { OptComponent } from './components/options-component';
import * as process from 'process';
import { Constants } from './utils/constants';


module.exports = (PluginHost: Application) => {
const app = PluginHost.owner;
const app = PluginHost.owner;

app.options.addComponent('options-component', OptComponent);

Expand All @@ -16,13 +16,13 @@ module.exports = (PluginHost: Application) => {

const processArgs = process.argv;
processArgs.forEach(command => {
if (command.indexOf(Constants.CONVERT_COMMAND) >= 0 ||
command.indexOf(Constants.SHORT_CONVERT_COMMAND) >= 0) {
if (command.indexOf(Constants.CONVERT_OPTION) >= 0 ||
command.indexOf(Constants.SHORT_CONVERT_OPTION) >= 0) {
startConverter = true;
}

if (command.indexOf(Constants.RENDER_COMMAND) >= 0 ||
command.indexOf(Constants.SHORT_RENDER_COMMAND) >= 0) {
if (command.indexOf(Constants.RENDER_OPTION) >= 0 ||
command.indexOf(Constants.SHORT_RENDER_OPTION) >= 0) {
startRenderer = true;
}
});
Expand All @@ -37,7 +37,7 @@ module.exports = (PluginHost: Application) => {
}

if (startRenderer) {
app.renderer.addComponent('render-component', RenderComponenet)
app.renderer.addComponent('render-component', RenderComponenet);
}
}

}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"lib": [
"es2017",
"dom"
], /* Specify library files to be included in the compilation. */
],
"resolveJsonModule": true, /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
Expand Down
17 changes: 10 additions & 7 deletions utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export class Constants {
// static readonly PLU
// JSON keys
static readonly PARAMS = 'parameters';
static readonly TAGS = 'tags';
Expand All @@ -7,13 +8,15 @@ export class Constants {
static readonly TEXT = 'text';
static readonly SHORT_TEXT = 'shortText';

// Commands
static readonly CONVERT_COMMAND = 'generate-json';
static readonly RENDER_COMMAND = 'generate-from-json';
static readonly SHORT_CONVERT_COMMAND = 'gen-json';
static readonly SHORT_RENDER_COMMAND = 'gen-from-json';
static readonly INCLUDE_TAGS_COMMAND = 'tags';
static readonly INCLUDE_PARAMS_COMMAND = 'params';
// Options
static readonly CONVERT_OPTION = 'generate-json';
static readonly RENDER_OPTION = 'generate-from-json';
static readonly SHORT_CONVERT_OPTION = 'gen-json';
static readonly SHORT_RENDER_OPTION = 'gen-from-json';
static readonly INCLUDE_TAGS_OPTION = 'tags';
static readonly INCLUDE_PARAMS_OPTION = 'params';
static readonly LOCALIZE_OPTION = 'localize'
static readonly TEMPLATE_STRINGS_OPTION = 'templateStrings';

static readonly GLOBAL_FUNCS_FILE_NAME = 'globalFunctions';
}
File renamed without changes.
2 changes: 1 addition & 1 deletion utils/factories/base-factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AttributeType } from "../enums/json-obj-kind";
import { AttributeType } from "../enums/json-keys";

export abstract class BaseFactory {
public name;
Expand Down
2 changes: 1 addition & 1 deletion utils/factories/class-factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseFactory } from './base-factory';
import { AttributeType } from '../enums/json-obj-kind';
import { AttributeType } from '../enums/json-keys';

const PROPERTIES_KEY = AttributeType[AttributeType.properties];
const METHODS_KEY = AttributeType[AttributeType.methods];
Expand Down
2 changes: 1 addition & 1 deletion utils/factories/enum-factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AttributeType } from "../enums/json-obj-kind";
import { AttributeType } from "../enums/json-keys";
import { BaseFactory } from "./base-factory";

const ENUM_MEMBER_KEY = AttributeType[AttributeType.members];
Expand Down
2 changes: 1 addition & 1 deletion utils/factories/interface-factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AttributeType } from "../enums/json-obj-kind";
import { AttributeType } from "../enums/json-keys";
import { BaseFactory } from "./base-factory";

const PROPERTIES_KEY = AttributeType[AttributeType.properties];
Expand Down
33 changes: 33 additions & 0 deletions utils/helpers/localize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as process from 'process';
import * as fs from 'fs-extra';
import { Constants } from '../constants';

export function localize(str) {
const value = getLocalizedValue(str);
if (value) {
return value;
}

return str;
}

function getLocalizedValue(str) {
const local = getOptionValue(Constants.LOCALIZE_OPTION);
const jsonFilePath = getOptionValue(Constants.TEMPLATE_STRINGS_OPTION);
const fileContent = fs.readJsonSync(jsonFilePath);

if (fileContent && fileContent[local]) {
return fileContent[local][str];
}

return str;
}

function getOptionValue(key) {
const indx = process.argv.findIndex((e) => e === `--${key}`);
if (indx >= process.argv.length - 1) {
return;
}

return process.argv[indx + 1];
}