Skip to content

Commit

Permalink
增加PLATFORM_ID和NGER_CONFIG token
Browse files Browse the repository at this point in the history
  • Loading branch information
meepobrother committed May 1, 2019
1 parent 3a61889 commit 4024d37
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 57 deletions.
11 changes: 5 additions & 6 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
"name": "nger",
"synchronize": true
},
"weapp": {
"outputDir": "addons",
"sourceDir": "src",
"templateDir": ".temp",
"npmDir": "npm"
}
"admin": "src/admin.ts",
"h5": "src/app.ts",
"pc": "src/pc.ts",
"weapp": "src/app.ts",
"alipay": "src/app.ts"
}
10 changes: 5 additions & 5 deletions packages/nger-cli/lib/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TypeContext, Type } from 'ims-decorator';
import { Injectable } from 'nger-core';
import { Injectable, PLATFORM_ID } from 'nger-core';
import ngerPlatformNode from 'nger-platform-node'
import gulp from 'gulp';
import chalk from 'chalk';
Expand Down Expand Up @@ -34,10 +34,10 @@ export class NgerCliBuild {
ios(context: TypeContext) { }
/** 后台 */
admin() {
console.log(`admin`)
platformBuildAdmin([]).bootstrapModule(NgerCliBuildAminBuilder).then(ref => {

})
platformBuildAdmin([{
provide: PLATFORM_ID,
useValue: 'admin'
}]).bootstrapModule(NgerCliBuildAminBuilder).then(ref => { })
}

async dev(name: string) {
Expand Down
10 changes: 9 additions & 1 deletion packages/nger-compiler-preact/lib/html.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// 将ng模板编译成preact可以执行的文件
import { NgModuleConfig } from './types'
import { Injector } from 'nger-di';
import { ComponentFactoryResolver } from 'nger-core';

// 需要将模板转换成preact
export class NgerCompilerPreactHtml {
constructor(public injector: Injector) { }
async run(config: NgModuleConfig) {

const { declarations } = config;
declarations.map(comp => {
console.log(comp)
});
}
}
16 changes: 7 additions & 9 deletions packages/nger-compiler-preact/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ import { NgerCompilerPreactHtml } from './html'
import { NgerCompilerPreactStyle } from './style'
import { NgerCompilerPreactTypescript } from './typescript'
import { NgerCompilerPreactAssets } from './assets'
import { StaticProvider } from 'nger-di';
import { StaticProvider, Injector } from 'nger-di';
import { NgerCompilerNgMetadata } from 'nger-compiler'
import { NgModuleBootstrap } from 'nger-core'
import { NgModuleBootstrap, NgerConfig } from 'nger-core'
const provider: StaticProvider[] = [{
provide: NgModuleBootstrap,
useExisting: NgerCompilerPreact,
multi: true
}, {
provide: NgerCompilerPreact,
useClass: NgerCompilerPreact,
deps: [
NgerCompilerPreactHtml,
NgerCompilerPreactStyle,
NgerCompilerPreactAssets,
NgerCompilerPreactTypescript,
NgerCompilerNgMetadata
]
NgerCompilerNgMetadata,
NgerConfig
],
multi: true
}, {
provide: NgerCompilerPreactHtml,
useClass: NgerCompilerPreactHtml,
deps: []
deps: [Injector]
}, {
provide: NgerCompilerPreactStyle,
useClass: NgerCompilerPreactStyle,
Expand Down
8 changes: 5 additions & 3 deletions packages/nger-compiler-preact/lib/preact.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NgModuleBootstrap, NgModuleRef } from 'nger-core';
import { NgModuleBootstrap, NgModuleRef, PLATFORM_ID, NgerConfig } from 'nger-core';
// 将ng模板编译成preact可以执行的文件
import { NgerCompilerPreactHtml } from './html'
import { NgerCompilerPreactStyle } from './style'
Expand All @@ -15,14 +15,16 @@ export class NgerCompilerPreact extends NgModuleBootstrap {
public style: NgerCompilerPreactStyle,
public assets: NgerCompilerPreactAssets,
public ts: NgerCompilerPreactTypescript,
public metadata: NgerCompilerNgMetadata
public metadata: NgerCompilerNgMetadata,
public config: NgerConfig
) {
super();
}
async run(ref: NgModuleRef<any>) {
const ngModule = ref.context.getClass(core.NgModuleMetadataKey) as core.NgModuleClassAst;
// 拿到ngModule的文件名
const fileName = ngModule.ast.metadataDef.fileName;
const platform = ref.injector.get(PLATFORM_ID);
const fileName = this.config[platform];
if (fileName) {
// 拿到ngModuleMetadata
const metadata = this.metadata.getMetadata(fileName);
Expand Down
16 changes: 9 additions & 7 deletions packages/nger-core/lib/platform/platform_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ChangeDetectorRef, DefaultChangeDetectorRef } from './change_detector_r
import { ApplicationRef } from './application_ref'
import { ComponentCreator } from './component_factory'
import { PLATFORM_INITIALIZER } from './application_tokens'
import { NGER_CONFIG, INgerConfig } from '../sdk/nger-config'
export const platformCore = createPlatformFactory(null, 'core', [{
provide: APP_INITIALIZER,
useValue: () => { },
Expand Down Expand Up @@ -82,19 +83,20 @@ export const platformCore = createPlatformFactory(null, 'core', [{
return new ScannerVisitor(scanner)
},
deps: [Injector]
}, {
provide: NGER_CONFIG,
useValue: {},
multi: true
}, {
provide: NgerConfig,
useFactory: () => {
const config = new NgerConfig();
config.watch = true;
config.loggerLevel = LoggerLevel.debug;
useFactory: (config: INgerConfig[]) => {
return new NgerConfig(config || []);
},
deps: [],
multi: true
deps: [NGER_CONFIG]
}, {
provide: Logger,
useFactory: (config: NgerConfig) => {
return new ConsoleLogger(config.loggerLevel || LoggerLevel.debug)
return new ConsoleLogger(config.get('loggerLevel') as LoggerLevel || LoggerLevel.debug)
},
deps: [NgerConfig]
}]);
39 changes: 38 additions & 1 deletion packages/nger-core/lib/sdk/nger-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export enum LoggerLevel {
error,
}
import { ConnectionOptions } from 'typeorm';
export class NgerConfig {
export interface INgerConfig {
// 是否监控文件变化
watch: boolean;
// 打印日志等级
Expand All @@ -20,4 +20,41 @@ export class NgerConfig {
};
// db
db: ConnectionOptions;
admin: string;
h5: string;
pc: string;
weapp: string;
alipay: string;
}
import { InjectionToken } from 'nger-di';
import { Inject } from '../decorators/inject';
type INgerConfigKey = keyof INgerConfig;
export const NGER_CONFIG = new InjectionToken<INgerConfig>(`NGER_CONFIG`)
export class NgerConfig {
config: any = {}
constructor(@Inject(NGER_CONFIG) configs: INgerConfig[]) {
if (Array.isArray(configs)) {
configs.map(cfg => {
this.config = {
...this.config,
...cfg
}
})
} else {
console.log(configs)
}
}

get(name: INgerConfigKey): INgerConfig[INgerConfigKey] {
const config: any = this.config[name];
if (config) {
return config
} else {
return config;
}
}

set(name: INgerConfigKey, value: INgerConfig[INgerConfigKey]) {
this.config[name] = value;
}
}
5 changes: 5 additions & 0 deletions packages/nger-core/lib/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export type DevModel = boolean;
export const DevModelToken = new InjectionToken<DevModel>(`DevModelToken`);
// 运行平台
export const PlatformToken = new InjectionToken<DevModel>(`PlatformToken`);
// 平台
export type PlatformId = 'lib' | 'h5' | 'prod' | 'wechat' | 'weapp' | 'alipay' | 'swap' | 'tt' | 'android' | 'ios' | 'admin'
export const PLATFORM_ID = new InjectionToken<PlatformId>(`PLATFORM_ID`)
// webapck打包
export const APP_ROOT = new InjectionToken<string>(`APP_ROOT`)
14 changes: 13 additions & 1 deletion packages/nger-platform-node/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPlatformFactory, Logger, FileSystem, Resolver } from 'nger-core'
import { createPlatformFactory, Logger, FileSystem, Resolver, NgerConfig, APP_ROOT, NGER_CONFIG } from 'nger-core'
import { NgerUtil } from 'nger-util'
import ngerPlatformAxios from 'nger-platform-axios'
import fs from 'fs-extra';
Expand All @@ -9,6 +9,18 @@ import {
ResolverFactory
} from 'enhanced-resolve';
export default createPlatformFactory(ngerPlatformAxios, 'node', [
{
provide: APP_ROOT,
useValue: process.cwd()
},
{
provide: NGER_CONFIG,
useFactory: (root: string) => {
return require(join(root, 'config/config.json'))
},
deps: [APP_ROOT],
multi: true
},
{
provide: NgerUtil,
useClass: NgerUtil,
Expand Down
33 changes: 9 additions & 24 deletions packages/nger-util/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { exec } from 'shelljs';
import { execSync } from 'child_process';
import { join } from 'path';
import { existsSync } from 'fs';
import { Logger, NgerConfig } from 'nger-core';
import { CompilerOptions } from 'typescript'
export class NgerUtil {
root: string = process.cwd()
constructor(public logger: Logger, public config: NgerConfig) { }
/** 加载配置文件 */
loadConfig(): NgerConfig {
const configPath = join(this.root, 'config/config.json');
if (this.config) {
return this.config;
}
if (existsSync(configPath)) {
this.config = require(join(this.root, 'config/config.json'));
}
return this.config;
}

getCompilerOptions(): CompilerOptions {
return require(join(this.root, 'tsconfig')).compilerOptions
Expand All @@ -42,20 +30,17 @@ export class NgerUtil {
}
/** 安装包 */
addPkg(name: string) {
let cfg = this.loadConfig();
let command: string = '';
if (!cfg) {
// cnpm 优先
cfg = cfg || { npm: 'yarn' } as any;
if (this.shouldUseYarn()) {
cfg.npm = 'yarn';
} else if (this.shouldUseCnpm()) {
cfg.npm = 'cnpm';
} else {
cfg.npm = 'npm';
}
// cnpm 优先
if (this.shouldUseYarn()) {
this.config.set('npm', 'yarn')
} else if (this.shouldUseCnpm()) {
this.config.set('npm', 'cnpm')
} else {
this.config.set('npm', 'npm')
}
switch (cfg.npm) {
const npm = this.config.get('npm');
switch (npm) {
case 'yarn':
command = `yarn add ${name}`
break;
Expand Down

0 comments on commit 4024d37

Please sign in to comment.