import { basename, dirname, extname, join, relative, sep } from 'path';
import { readdirSync, existsSync, writeFileSync, openSync, closeSync } from 'fs';
import { Logger } from '../logger/logger';
import { toUnixPath } from '../util/helpers';
import * as Constants from '../util/constants';
import * as GeneratorConstants from './constants';
import { camelCase, constantCase, getStringPropertyValue, mkDirpAsync, paramCase, pascalCase, readFileAsync, replaceAll, sentenceCase, upperCaseFirst, writeFileAsync } from '../util/helpers';
import { BuildContext } from '../util/interfaces';
import { globAll, GlobResult } from '../util/glob-util';
import { changeExtension, ensureSuffix, removeSuffix } from '../util/helpers';
import { appendNgModuleDeclaration, appendNgModuleExports, appendNgModuleProvider, insertNamedImportIfNeeded } from '../util/typescript-utils';
export function hydrateRequest(context: BuildContext, request: GeneratorRequest) {
const hydrated = request as HydratedGeneratorRequest;
const suffix = getSuffixFromGeneratorType(context, request.type);
hydrated.className = ensureSuffix(pascalCase(request.name), upperCaseFirst(suffix));
hydrated.fileName = removeSuffix(paramCase(request.name), `-${paramCase(suffix)}`);
if (request.type === 'pipe') hydrated.pipeName = camelCase(request.name);
if (!!hydrated.includeNgModule) {
if (hydrated.type === 'tabs') {
hydrated.importStatement = `import { IonicPage, NavController } from 'ionic-angular';`;
} else {
hydrated.importStatement = `import { IonicPage, NavController, NavParams } from 'ionic-angular';`;
}
hydrated.ionicPage = '\n@IonicPage()';
} else {
hydrated.ionicPage = null;
hydrated.importStatement = `import { NavController, NavParams } from 'ionic-angular';`;
}
hydrated.dirToRead = join(getStringPropertyValue(Constants.ENV_VAR_IONIC_ANGULAR_TEMPLATE_DIR), request.type);
const baseDir = getDirToWriteToByType(context, request.type);
hydrated.dirToWrite = join(baseDir, hydrated.fileName);
return hydrated;
}
export function createCommonModule(envVar: string, requestType: string) {
let className = requestType.charAt(0).toUpperCase() + requestType.slice(1) + 's';
let tmplt = `import { NgModule } from '@angular/core';\n@NgModule({\n\tdeclarations: [],\n\timports: [],\n\texports: []\n})\nexport class ${className}Module {}\n`;
writeFileSync(envVar, tmplt);
}
export function hydrateTabRequest(context: BuildContext, request: GeneratorTabRequest) {
const h = hydrateRequest(context, request);
const hydrated = Object.assign({
tabs: request.tabs,
tabContent: '',
tabVariables: '',
tabsImportStatement: '',
}, h) as HydratedGeneratorRequest;
if (hydrated.includeNgModule) {
hydrated.tabsImportStatement += `import { IonicPage, NavController } from 'ionic-angular';`;
} else {
hydrated.tabsImportStatement += `import { NavController } from 'ionic-angular';`;
}
for (let i = 0; i < request.tabs.length; i++) {
const tabVar = `${camelCase(request.tabs[i].name)}Root`;
if (hydrated.includeNgModule) {
hydrated.tabVariables += ` ${tabVar} = '${request.tabs[i].className}'\n`;
} else {
hydrated.tabVariables += ` ${tabVar} = ${request.tabs[i].className}\n`;
}
// If this is the last ion-tab to insert
// then we do not want a new line
if (i === request.tabs.length - 1) {
hydrated.tabContent += ` `;
} else {
hydrated.tabContent += ` \n`;
}
}
return hydrated;
}
export function readTemplates(pathToRead: string): Promise