77
88import { stringify } from "encoding/yaml.ts" ;
99import { join } from "path/mod.ts" ;
10+ import { existsSync } from "fs/mod.ts" ;
1011
1112import { Metadata } from "../config/types.ts" ;
12- import { mergeConfigs } from "../core/config.ts" ;
1313import { readYaml , readYamlFromString } from "../core/yaml.ts" ;
14- import {
15- kDefaultProjectPublishFile ,
16- projectPublishFile ,
17- } from "../project/project-shared.ts" ;
1814import { ProjectContext } from "../project/types.ts" ;
19- import { ProjectPublish } from "./types.ts" ;
15+ import { PublishDeployments , PublishRecord } from "./types.ts" ;
2016
21- export function projectPublishConfig (
17+ export function projectPublishDeployments (
2218 project : ProjectContext ,
23- ) : ProjectPublish {
24- const projectDir = project . dir ;
25- const publishFile = projectPublishFile ( projectDir ) ;
26- if ( publishFile ) {
27- return readYaml ( publishFile ) as ProjectPublish ;
19+ ) : PublishDeployments {
20+ const deplomentsFile = publishDeploymentsFile ( project . dir ) ;
21+ if ( deplomentsFile ) {
22+ return readYaml ( deplomentsFile ) as PublishDeployments ;
2823 } else {
29- return { } as ProjectPublish ;
24+ return { } as PublishDeployments ;
3025 }
3126}
3227
33- export function updateProjectPublishConfig (
34- target : ProjectContext ,
35- updateConfig : ProjectPublish ,
28+ export function recordProjectPublishDeployment (
29+ project : ProjectContext ,
30+ provider : string ,
31+ publish : PublishRecord ,
3632) {
33+ // read base config
3734 let indent = 2 ;
38- let baseConfig : Metadata = { } ;
39- const projectDir = target . dir ;
40- const publishFile = projectPublishFile ( projectDir ) ;
41- if ( publishFile ) {
42- const publishFileYaml = Deno . readTextFileSync ( publishFile ) ;
43- indent = detectIndentLevel ( publishFileYaml ) ;
44- baseConfig = readYamlFromString ( publishFileYaml ) as Metadata ;
35+ let deployments : PublishDeployments = { } ;
36+ const projectDir = project . dir ;
37+ const deploymentsFile = publishDeploymentsFile ( project . dir ) ;
38+ if ( deploymentsFile ) {
39+ const deploymentsFileYaml = Deno . readTextFileSync ( deploymentsFile ) ;
40+ indent = detectIndentLevel ( deploymentsFileYaml ) ;
41+ deployments = readYamlFromString ( deploymentsFileYaml ) as PublishDeployments ;
42+ }
43+
44+ // update as required
45+ if ( deployments [ provider ] ) {
46+ const deploymentIdx = deployments [ provider ] . findIndex (
47+ ( published ) => published . id === publish . id ,
48+ ) ;
49+ if ( deploymentIdx !== - 1 ) {
50+ deployments [ provider ] [ deploymentIdx ] = publish ;
51+ } else {
52+ deployments [ provider ] . push ( publish ) ;
53+ }
54+ } else {
55+ deployments [ provider ] = [ publish ] ;
4556 }
46- const updatedConfig = mergeConfigs ( baseConfig , updateConfig ) ;
57+
4758 Deno . writeTextFileSync (
48- publishFile || join ( projectDir , kDefaultProjectPublishFile ) ,
49- stringifyPublishConfig ( updatedConfig , indent ) ,
59+ deploymentsFile || join ( projectDir , kDefaultPublishDeploymetsFile ) ,
60+ stringifyPublishConfig ( deployments , indent ) ,
5061 ) ;
5162}
5263
@@ -65,3 +76,11 @@ function detectIndentLevel(yaml: string) {
6576 const spaceMatch = yaml . match ( / \n ( \s + ) / ) ;
6677 return spaceMatch ? spaceMatch [ 1 ] . length : 2 ;
6778}
79+
80+ const kDefaultPublishDeploymetsFile = "_publish.yml" ;
81+
82+ export function publishDeploymentsFile ( dir : string ) : string | undefined {
83+ return [ kDefaultPublishDeploymetsFile , "_publish.yaml" ]
84+ . map ( ( file ) => join ( dir , file ) )
85+ . find ( existsSync ) ;
86+ }
0 commit comments