chore: more base configuration

This commit is contained in:
Igor Randjelovic
2020-11-19 21:45:16 +01:00
parent 4518cfa31b
commit 72a87c5d2c
18 changed files with 492 additions and 173 deletions

View File

@@ -0,0 +1,51 @@
import { env } from '../index';
import { resolve, basename } from 'path';
export function getProjectRootPath(): string {
// todo: find actual path?
return process.cwd();
//__dirname
}
export function getAbsoluteDistPath() {
return resolve(getProjectRootPath(), getDistPath());
}
export function getEntryPath() {
const packageJson = getPackageJson();
return resolve(packageJson.main);
}
export function getDistPath() {
if (env.ios) {
const appName = basename(getProjectRootPath());
return `platforms/ios/${appName}/app`;
}
if (env.android) {
return `platforms/android/app/src/main/assets/app`;
}
// todo: additional platforms
// perhaps we could combine platform specifics into "plugins"
// 3rd party platforms would be treated the same
}
interface IPackageJson {
main?: string;
dependencies?: {
[name: string]: string;
};
devDependencies?: {
[name: string]: string;
};
// todo: add additional fields as we require them
}
export function getPackageJson() {
const packageJsonPath = resolve(getProjectRootPath(), 'package.json');
return require(packageJsonPath) as IPackageJson;
}

View File

@@ -1,40 +0,0 @@
import { existsSync } from 'fs';
import { resolve } from 'path';
import { IWebpackEnv } from '@nativescript/webpack';
export function getDistPath(env: IWebpackEnv) {
if (env.ios) {
return `platforms/ios/[todo]/app`;
}
if (env.android) {
return `platforms/android/app/src/main/assets/app`;
}
// todo: additional platforms
// perhaps we could combine platform specifics into "plugins"
// 3rd party platforms would be treated the same
}
export function getPackageJson(projectDir: string) {
const packageJsonPath = getPackageJsonPath(projectDir);
const result = readJsonFile(packageJsonPath);
return result;
}
export function readJsonFile(filePath: string) {
return require(filePath) as {
main: string;
// to be extended?
};
}
export function getPackageJsonPath(projectDir: string) {
const packagePath = resolve(projectDir, 'package.json');
if (existsSync(packagePath)) {
return packagePath;
} else {
return getPackageJsonPath(resolve(projectDir, '..'));
}
}

View File

@@ -1,5 +1,5 @@
import { existsSync } from 'fs';
import { getPackageJson } from './projectHelpers';
import { getPackageJson } from './project';
import { resolve } from 'path';
// todo: get rid of these or reduce them to their simplest form
@@ -20,12 +20,12 @@ function verifyEntryModuleDirectory(appDirectory: string) {
}
}
function getPackageJsonEntry(appDirectory) {
const packageJsonSource = getPackageJson(appDirectory);
function getPackageJsonEntry() {
const packageJsonSource = getPackageJson();
const entry = packageJsonSource.main;
if (!entry) {
throw new Error(`${appDirectory}/package.json must contain a 'main' attribute!`);
throw new Error(`package.json must contain a 'main' attribute!`);
}
return entry.replace(/\.js$/i, '');
@@ -34,7 +34,7 @@ function getPackageJsonEntry(appDirectory) {
export function getEntryModule(appDirectory: string, platform: 'android' | 'ios') {
verifyEntryModuleDirectory(appDirectory);
const entry = getPackageJsonEntry(appDirectory);
const entry = getPackageJsonEntry();
const tsEntryPath = resolve(appDirectory, `${entry}.ts`);
const jsEntryPath = resolve(appDirectory, `${entry}.js`);