1+ const FS = require ( "fs-extra" )
2+ const Path = require ( "path" )
3+ const KlawSync = require ( 'klaw-sync' )
4+ const RandomString = require ( "../Lib/RandomString.js" )
5+ const DependencyParser = require ( "../Lib/DependencyParser.js" )
6+ const Tar = require ( "tar" )
7+
8+ async function FixDependencyVersions ( Dependencies ) {
9+ let Updated = false
10+ const VersionFixes = [ ]
11+
12+ let DependencyIndex = - 1
13+ for ( const Dependency of Dependencies ) {
14+ let ParsedDependency = DependencyParser . Parse ( Dependency )
15+ DependencyIndex ++
16+
17+ if ( ParsedDependency . Version != undefined ) { continue }
18+
19+ Updated = true
20+ TypeWriter . Logger . Warning ( `No version specified for ${ ParsedDependency . FullName } , fixing...` )
21+ VersionFixes . push (
22+ [
23+ DependencyIndex ,
24+ ParsedDependency ,
25+ async function ( DependencyIndex , ParsedDependency ) {
26+ const LatestVersion = await TypeWriter . DependencyManager . GetLatestVersion ( ParsedDependency )
27+ ParsedDependency . Version = LatestVersion
28+ Dependencies [ DependencyIndex ] = DependencyParser . Format ( ParsedDependency )
29+ }
30+ ]
31+
32+ )
33+
34+ }
35+
36+ await Promise . all ( VersionFixes . map ( D => D [ 2 ] ( D [ 0 ] , D [ 1 ] ) ) )
37+
38+ return [ Updated , Dependencies ]
39+ }
40+
41+ async function ScanCode ( ScanFolder , Extension ) {
42+ ScanFolder = Path . normalize ( ScanFolder )
43+ const ScannedCode = { }
44+ const Files = KlawSync ( ScanFolder , { nodir : true } )
45+ for ( const File of Files ) {
46+ const FilePath = File . path
47+ const CodePath = FilePath . split ( ScanFolder ) [ 1 ] . replaceAll ( "\\" , "/" ) . replaceAll ( "/" , "." ) . substring ( 1 ) . split ( "." ) . slice ( 0 , - 1 ) . join ( "." )
48+
49+ ScannedCode [ CodePath ] = {
50+ Type : Extension ,
51+ Code : encodeURIComponent ( FS . readFileSync ( FilePath , "utf-8" ) )
52+ }
53+
54+ if ( CodePath . endsWith ( ".Main" ) ) {
55+ ScannedCode [ CodePath . substring ( 0 , CodePath . length - 5 ) ] = {
56+ Type : "Redirect" ,
57+ Path : CodePath
58+ }
59+ } else if ( CodePath . endsWith ( ".Index" ) ) {
60+ ScannedCode [ CodePath . substring ( 0 , CodePath . length - 6 ) ] = {
61+ Type : "Redirect" ,
62+ Path : CodePath
63+ }
64+ }
65+ }
66+
67+ return ScannedCode
68+ }
69+
70+ class Builder {
71+ constructor ( Folder , Branch , OutputPath ) {
72+ this . BuildFolder = Folder
73+ this . BranchFolder = Path . normalize ( `${ Folder } /${ Branch } /` )
74+ this . OutputPath = OutputPath
75+ this . BuildId = RandomString ( 32 )
76+ this . BuildFolder = Path . normalize ( `${ TypeWriter . Folders . Cache . BuildCacheFolder } /${ this . BuildId } /` )
77+ this . PackageInfoFile = Path . normalize ( `${ this . BranchFolder } /package.info.json` )
78+
79+ TypeWriter . Logger . Debug ( `Building to ${ this . BuildFolder } ` )
80+
81+ FS . ensureDirSync ( this . BuildFolder )
82+ FS . cpSync ( this . BranchFolder + "/package.info.json" , this . BuildFolder + "/package.info.json" )
83+
84+ this . CreateRequiredFolders ( )
85+ }
86+
87+ CreateRequiredFolders ( ) {
88+ FS . ensureDirSync ( this . BranchFolder + "/js" )
89+ FS . ensureDirSync ( this . BranchFolder + "/lua" )
90+ FS . ensureDirSync ( this . BranchFolder + "/resources" )
91+ }
92+
93+ async ValidatePackageInfo ( ) {
94+ const PackageInfo = FS . readJsonSync ( this . PackageInfoFile )
95+ let NeedsUpdate = false
96+
97+ const [ Updated , Dependencies ] = await FixDependencyVersions ( PackageInfo . Dependencies )
98+ NeedsUpdate = Updated || NeedsUpdate
99+ PackageInfo . Dependencies = Dependencies
100+
101+ if ( NeedsUpdate ) {
102+ FS . writeJsonSync ( this . PackageInfoFile , PackageInfo , { spaces : 4 } )
103+ FS . writeJSONSync ( this . BuildFolder + "/package.info.json" , PackageInfo , { spaces : 4 } )
104+ }
105+ }
106+
107+ async ScanCode ( ) {
108+ const ScannedCode = Object . assign (
109+ { } ,
110+ await ScanCode ( this . BranchFolder + "/lua" , "lua" ) ,
111+ await ScanCode ( this . BranchFolder + "/js" , "js" )
112+ )
113+
114+ FS . writeJSONSync (
115+ `${ this . BuildFolder } /Code.json` ,
116+ ScannedCode ,
117+ {
118+ spaces : "\t"
119+ }
120+ )
121+ }
122+
123+ async ScanResources ( ) {
124+ const ResourceFolder = Path . normalize ( `${ this . BranchFolder } /resources` )
125+ const DestinationResourceFolder = `${ this . BuildFolder } /resources/`
126+
127+ const ResourceIndex = KlawSync (
128+ this . BranchFolder + "/resources" ,
129+ {
130+ nodir : true
131+ }
132+ ) . map (
133+ ( File ) => {
134+ const FilePath = File . path
135+ const ResourceFilePath = Path . normalize ( FilePath . split ( ResourceFolder ) [ 1 ] ) . replaceAll ( "\\" , "/" )
136+ return ResourceFilePath
137+ }
138+ )
139+
140+ FS . copySync ( ResourceFolder , DestinationResourceFolder )
141+ FS . writeJSONSync (
142+ `${ this . BuildFolder } /ResourceIndex.json` ,
143+ ResourceIndex ,
144+ {
145+ spaces : "\t"
146+ }
147+ )
148+ }
149+
150+ async Compress ( ) {
151+ const PackageInfo = FS . readJsonSync ( this . PackageInfoFile )
152+ const OutputFile = Path . join ( this . OutputPath + `/${ PackageInfo . Id } .twr` )
153+ TypeWriter . Logger . Debug ( `Outputting to ${ OutputFile } in ${ this . BuildFolder } ` )
154+ Tar . create (
155+ {
156+ file : OutputFile ,
157+ cwd : this . BuildFolder ,
158+ sync : true ,
159+ noMtime : true ,
160+ portable : true
161+ } ,
162+ FS . readdirSync ( this . BuildFolder )
163+ )
164+
165+ return OutputFile
166+ }
167+
168+ async Cleanup ( ) {
169+ FS . rmSync ( this . BuildFolder , { recursive : true , force : true } )
170+ }
171+ }
172+
173+ module . exports = Builder
0 commit comments