Skip to content
Draft
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
6 changes: 4 additions & 2 deletions bin/unibuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ async function findAndLoadConfig(): Promise<Config> {
for (const filename of configFilenames) {
const fullPath = resolve(cwd, filename);
if (existsSync(fullPath)) {
const configModule = await import(pathToFileURL(fullPath).href);
const fileURL = pathToFileURL(fullPath).href;
console.log('Importing: ', fileURL);
const configModule = await import(fileURL);
return configModule.default;
}
}
Expand All @@ -23,7 +25,7 @@ async function findAndLoadConfig(): Promise<Config> {
(async () => {
try {
const config = await findAndLoadConfig();
new CLI(config).run();
await new CLI(config).run();
} catch (err) {
console.error('[unibuild] Error:', err);
process.exit(1);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"dependencies": {
"chalk": "^5.3.0",
"commander": "^12.1.0",
"execa": "^9.6.0",
"ora": "^8.2.0",
"tsx": "^4.10.5"
},
"devDependencies": {
Expand Down
76 changes: 45 additions & 31 deletions src/asset.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import AssetFile from './asset_file';
import AssetInput from './asset_input';
import BuildFunction from './build_function';
import AssetOptions from './asset_options';
import { generateCommand } from './cmd';
import IAsset from './i_asset';

class Asset {
class Asset implements IAsset {
buildFunction?: BuildFunction;

command?: string;

input: AssetInput[];
input: IAsset[];

name: string;

Expand All @@ -30,7 +30,7 @@ class Asset {
}
}

static normalizeInput(inputs: string | AssetInput | (string | AssetInput)[]): AssetInput[] {
static normalizeInput(inputs: string | IAsset | (string | IAsset)[]): IAsset[] {
return [inputs].flat().map((input) => {
if (typeof input === 'string') {
return new AssetFile(input);
Expand All @@ -40,64 +40,78 @@ class Asset {
});
}

get path() {
get path(): string {
return this.outfile.path;
}

hasAssetDependencies() {
hasAssetDependencies(): boolean {
return this.input.some((input) => input instanceof Asset);
}

needsBuilding(options: Partial<{ force: boolean, release: boolean }>) {
return (!this.releaseOnly || options.release) &&
(options.force || this.outfileMissing() || this.inputChanged());
async needsBuilding(options: Partial<{ force: boolean, release: boolean }>): Promise<boolean> {
const outfileMissing = await this.outfileMissing();
const inputChanged = await this.inputChanged();

return (!this.releaseOnly || !!options.release) && (options.force || outfileMissing || inputChanged);
}

needsRebuild() {
return !this.outfileExists() || this.inputChanged() || this.inputNeedsRebuild();
async needsRebuild(): Promise<boolean> {
return !(await this.outfileExists()) || (await this.inputChanged()) || (await this.inputNeedsRebuild());
}

exists() {
return this.outfileExists();
async exists(): Promise<boolean> {
return await this.outfileExists();
}

isFile() {
return this.outfile.isFile();
async isFile(): Promise<boolean> {
return await this.outfile.isFile();
}

outfileMissing() {
return !this.outfileExists();
async outfileMissing(): Promise<boolean> {
return !(await this.outfileExists());
}

outfileExists() {
return this.outfile.exists();
async outfileExists(): Promise<boolean> {
return await this.outfile.exists();
}

modifiedTime() {
return this.outfile.modifiedTime();
async modifiedTime(): Promise<number> {
return await this.outfile.modifiedTime();
}

inputChanged() {
return this.input.some((input) => input.newerThan(this));
async inputChanged(): Promise<boolean> {
for (const input of this.input) {
if (await input.newerThan(this)) {
return true;
}
}

return false;
}

inputNeedsRebuild() {
return this.input.some((input) => input.needsRebuild());
async inputNeedsRebuild(): Promise<boolean> {
for (const input of this.input) {
if (await input.needsRebuild()) {
return true;
}
}

return false;
}

newerThan(other: AssetInput) {
return this.outfile.newerThan(other);
async newerThan(other: IAsset): Promise<boolean> {
return await this.outfile.newerThan(other);
}

canBeBuilt() {
async canBeBuilt(): Promise<boolean> {
return true;
}

read() {
return this.outfile.read();
async read(): Promise<string> {
return await this.outfile.read();
}

toString() {
toString(): string {
return this.outfile.toString();
}
}
Expand Down
52 changes: 30 additions & 22 deletions src/asset_file.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,66 @@
import * as fs from 'fs';
import AssetInput from './asset_input';
import * as fs from 'fs/promises';
import IAsset from './i_asset';

class AssetFile {
class AssetFile implements IAsset {
path: string;

constructor(path: string) {
this.path = path;
}

modifiedTime(): number {
if (!this.exists()) {
async modifiedTime(): Promise<number> {
if (!(await this.exists())) {
return 0;
}

return fs.statSync(this.path).mtimeMs;
const stats = await fs.stat(this.path);
return stats.mtimeMs;
}

newerThan(other: AssetInput) {
return this.modifiedTime() > other.modifiedTime();
async newerThan(other: IAsset): Promise<boolean> {
return (await this.modifiedTime()) > (await other.modifiedTime());
}

exists() {
return fs.existsSync(this.path);
async exists(): Promise<boolean> {
try {
await fs.access(this.path);
return true;
} catch {
return false;
}
}

canBeBuilt() {
async canBeBuilt(): Promise<boolean> {
return false;
}

needsBuilding() {
async needsBuilding(): Promise<boolean> {
return false;
}

needsRebuild() {
async needsRebuild(): Promise<boolean> {
return false;
}

isFile() {
return fs.lstatSync(this.path).isFile();
async isFile(): Promise<boolean> {
const stats = await fs.lstat(this.path);
return stats.isFile();
}

read() {
return fs.readFileSync(this.path).toString();
async read(): Promise<string> {
const data = await fs.readFile(this.path);
return data.toString();
}

write(contents: string) {
fs.writeFileSync(this.path, contents);
async write(contents: string): Promise<void> {
await fs.writeFile(this.path, contents);
}

remove() {
fs.unlinkSync(this.path);
async remove(): Promise<void> {
await fs.unlink(this.path);
}

toString() {
toString(): string {
return this.path;
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/asset_input.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/asset_options.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import AssetInput from './asset_input';
import BuildFunction from './build_function';
import { CommandGenerator } from './cmd';
import Asset from './asset';
import IAsset from './i_asset';

interface AssetOptions {
build?: BuildFunction;
command?: string | string[] | CommandGenerator<Asset>;
input: string | AssetInput | (string | AssetInput)[];
input: string | IAsset | (string | IAsset)[];
outfile: string;
releaseOnly?: boolean;
}
Expand Down
2 changes: 1 addition & 1 deletion src/build_function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BuildOptions from './build_options';

type BuildFunction = (options: Partial<BuildOptions>, ...args: string[]) => string;
type BuildFunction = (options: BuildOptions, ...args: string[]) => string;

export default BuildFunction;
5 changes: 3 additions & 2 deletions src/build_stages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Asset from './asset';
import AssetInput from './asset_input';
import IAsset from './i_asset';


class BuildStages {
grouping: string[][] = [];
Expand All @@ -14,7 +15,7 @@ class BuildStages {
assets.forEach((asset: Asset) => {
if (visited.has(asset.name)) return;

if (asset.input.every((input: AssetInput) => {
if (asset.input.every((input: IAsset) => {
if (!input.canBeBuilt()) return true;

const inputAsset = input as Asset;
Expand Down
Loading