Skip to content

Commit

Permalink
Refresh build info on clean compile, add Flamework version warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Fireboltofdeath committed Jul 18, 2021
1 parent 29c52cb commit 298df43
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/classes/buildInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from "fs";
import ajv from "ajv";
import crypto from "crypto";
import { v4 as uuid } from "uuid";
import { PACKAGE_ROOT } from "./rojoResolver/constants";
import { PACKAGE_ROOT, PKG_VERSION } from "./rojoResolver/constants";

interface BuildDecorator {
name: string;
Expand Down Expand Up @@ -109,10 +109,9 @@ export class BuildInfo {
private identifiersLookup = new Map<string, string>();
constructor(public buildInfoPath: string, buildInfo?: FlameworkBuildInfo) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const flameworkConfig = require(path.join(PACKAGE_ROOT, "package.json"));
this.buildInfo = buildInfo ?? {
version: 1,
flameworkVersion: flameworkConfig.version,
flameworkVersion: PKG_VERSION,
identifiers: {},
};
if (buildInfo) {
Expand Down Expand Up @@ -141,6 +140,13 @@ export class BuildInfo {
return salt;
}

/**
* Retrieves the version of flamework that this project was originally compiled on.
*/
getFlameworkVersion() {
return this.buildInfo.flameworkVersion;
}

/**
* Register a build info from an external source, normally packages.
* @param buildInfo The BuildInfo to add
Expand Down
2 changes: 1 addition & 1 deletion src/classes/rojoResolver/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const LIB_PATH = path.join(PACKAGE_ROOT, "lib");

// intentionally not using PACKAGE_ROOT because playground has webpack issues
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
export const COMPILER_VERSION: string = require("../../../package.json").version;
export const PKG_VERSION: string = require("../../../package.json").version;

export const NODE_MODULES = "node_modules";
export const RBXTS_SCOPE = "@rbxts";
Expand Down
5 changes: 4 additions & 1 deletion src/classes/transformState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Hashids from "hashids";
import { ClassInfo } from "../types/classes";
import { CallMacro } from "../transformations/macros/macro";
import { CALL_MACROS } from "../transformations/macros/call/callMacros";
import { isCleanBuildDirectory } from "../util/functions/isCleanBuildDirectory";

const IGNORE_RBXTS_REGEX = /node_modules\/@rbxts\/(compiler-types|types)\/.*\.d\.ts$/;

Expand Down Expand Up @@ -78,7 +79,7 @@ export class TransformState {

private setupBuildInfo() {
let baseBuildInfo = BuildInfo.fromDirectory(this.currentDirectory);
if (!baseBuildInfo) {
if (!baseBuildInfo || (Cache.isInitialCompile && isCleanBuildDirectory(this.options))) {
if (this.options.incremental && this.options.tsBuildInfoFile) {
if (ts.sys.fileExists(this.options.tsBuildInfoFile)) {
throw new Error(`Flamework cannot be built in a dirty environment, please delete your tsbuildinfo`);
Expand Down Expand Up @@ -152,6 +153,8 @@ export class TransformState {

this.packageName = packageJson.name;
this.isGame = !this.packageName.startsWith("@");

Cache.isInitialCompile = false;
}

private areMacrosSetup = false;
Expand Down
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {} from "ts-expose-internals";
import ts from "typescript";
import path from "path";
import { transformFile } from "./transformations/transformFile";
import { TransformerConfig, TransformState } from "./classes/transformState";
import { Logger } from "./classes/logger";
import { viewFile } from "./information/viewFile";
import { f } from "./util/factory";
import chalk from "chalk";
import { PKG_VERSION } from "./classes/rojoResolver/constants";

export default function (program: ts.Program, config?: TransformerConfig) {
return (context: ts.TransformationContext): ((file: ts.SourceFile) => ts.Node) => {
Expand All @@ -15,13 +17,25 @@ export default function (program: ts.Program, config?: TransformerConfig) {
const state = new TransformState(program, context, config ?? {});
let hasCollectedInformation = false;

const projectFlameworkVersion = state.buildInfo.getFlameworkVersion();
if (projectFlameworkVersion !== PKG_VERSION) {
Logger.writeLine(
`${chalk.red("Project was compiled on different version of Flamework.")}`,
`Please recompile by deleting the ${path.relative(state.currentDirectory, state.outDir)} directory`,
`Current Flamework Version: ${chalk.yellow(PKG_VERSION)}`,
`Previous Flamework Version: ${chalk.yellow(projectFlameworkVersion)}`,
);
process.exit(1);
}

return (file: ts.SourceFile) => {
if (!ts.isSourceFile(file)) {
throw Logger.writeLine(
Logger.writeLine(
`${chalk.red("Failed to load! TS version mismatch detected")}`,
"It is recommended that you use a local install of roblox-ts.",
`You can install a local version using ${chalk.green("npm install -D roblox-ts")}`,
);
process.exit(1);
}

if (!hasCollectedInformation) {
Expand Down
2 changes: 2 additions & 0 deletions src/util/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Cache {
rojoSum?: string;
rojoResolver?: RojoResolver;
buildInfoCandidates?: string[];
isInitialCompile: boolean;
shouldView: Map<string, boolean>;
realPath: Map<string, string>;
}
Expand All @@ -12,6 +13,7 @@ export interface Cache {
* Global cache that is only reset when rbxtsc is restarted.
*/
export const Cache: Cache = {
isInitialCompile: true,
shouldView: new Map(),
realPath: new Map(),
};
10 changes: 10 additions & 0 deletions src/util/functions/isCleanBuildDirectory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fs from "fs";
import ts from "typescript";

export function isCleanBuildDirectory(compilerOptions: ts.CompilerOptions) {
if (compilerOptions.incremental && compilerOptions.tsBuildInfoFile) {
return !fs.existsSync(compilerOptions.tsBuildInfoFile);
}

return true;
}

0 comments on commit 298df43

Please sign in to comment.