|
1 |
| -import fs = require("fs"); |
2 | 1 | import path = require("path");
|
3 | 2 | import * as vscode from "vscode";
|
4 | 3 | import { AtelierAPI } from "../api";
|
5 | 4 | import { config, explorerProvider, OBJECTSCRIPT_FILE_SCHEMA, schemas } from "../extension";
|
6 |
| -import { currentFile, mkdirSyncRecursive, notNull, outputChannel, uriOfWorkspaceFolder } from "../utils"; |
| 5 | +import { currentFile, fileExists, notNull, outputChannel, uriOfWorkspaceFolder } from "../utils"; |
7 | 6 | import { NodeBase } from "../explorer/models/nodeBase";
|
8 | 7 |
|
9 | 8 | export const getCategory = (fileName: string, addCategory: any | boolean): string => {
|
@@ -99,92 +98,96 @@ export async function exportFile(
|
99 | 98 | const api = new AtelierAPI(workspaceFolder);
|
100 | 99 | api.setNamespace(namespace);
|
101 | 100 | const log = (status) => outputChannel.appendLine(`export "${name}" as "${fileName}" - ${status}`);
|
102 |
| - const folders = path.dirname(fileName); |
103 |
| - return mkdirSyncRecursive(folders) |
104 |
| - .then(() => { |
105 |
| - return api.getDoc(name).then((data) => { |
106 |
| - if (!data || !data.result) { |
107 |
| - throw new Error("Something wrong happened"); |
108 |
| - } |
109 |
| - const content = data.result.content; |
110 |
| - const { noStorage, dontExportIfNoChanges } = config("export"); |
| 101 | + const foldersUri = vscode.Uri.file(path.dirname(fileName)); |
| 102 | + try { |
| 103 | + if (!(await fileExists(foldersUri))) { |
| 104 | + // Only attempt to create directories that don't exist |
| 105 | + await vscode.workspace.fs.createDirectory(foldersUri); |
| 106 | + } |
111 | 107 |
|
112 |
| - const promise = new Promise((resolve, reject) => { |
113 |
| - if (noStorage) { |
114 |
| - // get only the storage xml for the doc. |
115 |
| - api.getDoc(name + "?storageOnly=1").then((storageData) => { |
116 |
| - if (!storageData || !storageData.result) { |
117 |
| - reject(new Error("Something wrong happened fetching the storage data")); |
118 |
| - } |
119 |
| - const storageContent = storageData.result.content; |
| 108 | + api.getDoc(name).then((data) => { |
| 109 | + if (!data || !data.result) { |
| 110 | + throw new Error("Something wrong happened"); |
| 111 | + } |
| 112 | + const content = data.result.content; |
| 113 | + const { noStorage, dontExportIfNoChanges } = config("export"); |
120 | 114 |
|
121 |
| - if (storageContent.length > 1 && storageContent[0] && storageContent.length < content.length) { |
122 |
| - const storageContentString = storageContent.join("\n"); |
123 |
| - const contentString = content.join("\n"); |
| 115 | + const promise = new Promise((resolve, reject) => { |
| 116 | + if (noStorage) { |
| 117 | + // get only the storage xml for the doc. |
| 118 | + api.getDoc(name + "?storageOnly=1").then((storageData) => { |
| 119 | + if (!storageData || !storageData.result) { |
| 120 | + reject(new Error("Something wrong happened fetching the storage data")); |
| 121 | + } |
| 122 | + const storageContent = storageData.result.content; |
124 | 123 |
|
125 |
| - // find and replace the docs storage section with '' |
126 |
| - resolve({ |
127 |
| - content: contentString.replace(storageContentString, ""), |
128 |
| - found: contentString.indexOf(storageContentString) >= 0, |
129 |
| - }); |
130 |
| - } else { |
131 |
| - resolve({ found: false }); |
132 |
| - } |
133 |
| - }); |
134 |
| - } else { |
135 |
| - resolve({ found: false }); |
136 |
| - } |
137 |
| - }); |
| 124 | + if (storageContent.length > 1 && storageContent[0] && storageContent.length < content.length) { |
| 125 | + const storageContentString = storageContent.join("\n"); |
| 126 | + const contentString = content.join("\n"); |
| 127 | + |
| 128 | + // find and replace the docs storage section with '' |
| 129 | + resolve({ |
| 130 | + content: contentString.replace(storageContentString, ""), |
| 131 | + found: contentString.indexOf(storageContentString) >= 0, |
| 132 | + }); |
| 133 | + } else { |
| 134 | + resolve({ found: false }); |
| 135 | + } |
| 136 | + }); |
| 137 | + } else { |
| 138 | + resolve({ found: false }); |
| 139 | + } |
| 140 | + }); |
138 | 141 |
|
139 |
| - return promise |
140 |
| - .then((res: any) => { |
141 |
| - if (Buffer.isBuffer(content)) { |
142 |
| - // This is a binary file |
143 |
| - let isSkipped = ""; |
144 |
| - if (dontExportIfNoChanges && fs.existsSync(fileName)) { |
145 |
| - const existingContent = fs.readFileSync(fileName); |
146 |
| - if (content.equals(existingContent)) { |
147 |
| - fs.writeFileSync(fileName, content); |
148 |
| - } else { |
149 |
| - isSkipped = " => skipped - no changes."; |
150 |
| - } |
| 142 | + return promise |
| 143 | + .then(async (res: any) => { |
| 144 | + const fileUri = vscode.Uri.file(fileName); |
| 145 | + if (Buffer.isBuffer(content)) { |
| 146 | + // This is a binary file |
| 147 | + let isSkipped = ""; |
| 148 | + if (dontExportIfNoChanges && (await fileExists(fileUri))) { |
| 149 | + const existingContent = await vscode.workspace.fs.readFile(fileUri); |
| 150 | + if (content.equals(existingContent)) { |
| 151 | + await vscode.workspace.fs.writeFile(fileUri, content); |
151 | 152 | } else {
|
152 |
| - fs.writeFileSync(fileName, content); |
| 153 | + isSkipped = " => skipped - no changes."; |
153 | 154 | }
|
154 |
| - log(`Success ${isSkipped}`); |
155 | 155 | } else {
|
156 |
| - // This is a text file |
157 |
| - let joinedContent = content.join("\n"); |
158 |
| - let isSkipped = ""; |
| 156 | + await vscode.workspace.fs.writeFile(fileUri, content); |
| 157 | + } |
| 158 | + log(`Success ${isSkipped}`); |
| 159 | + } else { |
| 160 | + // This is a text file |
| 161 | + let joinedContent = content.join("\n"); |
| 162 | + let isSkipped = ""; |
159 | 163 |
|
160 |
| - if (res.found) { |
161 |
| - joinedContent = res.content.toString("utf8"); |
162 |
| - } |
| 164 | + if (res.found) { |
| 165 | + joinedContent = res.content.toString("utf8"); |
| 166 | + } |
163 | 167 |
|
164 |
| - if (dontExportIfNoChanges && fs.existsSync(fileName)) { |
165 |
| - const existingContent = fs.readFileSync(fileName, "utf8"); |
166 |
| - // stringify to harmonise the text encoding. |
167 |
| - if (JSON.stringify(joinedContent) !== JSON.stringify(existingContent)) { |
168 |
| - fs.writeFileSync(fileName, joinedContent); |
169 |
| - } else { |
170 |
| - isSkipped = " => skipped - no changes."; |
171 |
| - } |
| 168 | + if (dontExportIfNoChanges && (await fileExists(fileUri))) { |
| 169 | + const existingContent = new TextDecoder().decode(await vscode.workspace.fs.readFile(fileUri)); |
| 170 | + // stringify to harmonise the text encoding. |
| 171 | + if (JSON.stringify(joinedContent) !== JSON.stringify(existingContent)) { |
| 172 | + await vscode.workspace.fs.writeFile(fileUri, new TextEncoder().encode(joinedContent)); |
172 | 173 | } else {
|
173 |
| - fs.writeFileSync(fileName, joinedContent); |
| 174 | + isSkipped = " => skipped - no changes."; |
174 | 175 | }
|
175 |
| - |
176 |
| - log(`Success ${isSkipped}`); |
| 176 | + } else { |
| 177 | + await vscode.workspace.fs.writeFile(fileUri, new TextEncoder().encode(joinedContent)); |
177 | 178 | }
|
178 |
| - }) |
179 |
| - .catch((error) => { |
180 |
| - throw error; |
181 |
| - }); |
182 |
| - }); |
183 |
| - }) |
184 |
| - .catch((error) => { |
185 |
| - log("ERROR: " + error); |
186 |
| - throw error; |
| 179 | + |
| 180 | + log(`Success ${isSkipped}`); |
| 181 | + } |
| 182 | + }) |
| 183 | + .catch((error) => { |
| 184 | + throw error; |
| 185 | + }); |
187 | 186 | });
|
| 187 | + } catch (error) { |
| 188 | + log("ERROR: " + error); |
| 189 | + throw error; |
| 190 | + } |
188 | 191 | }
|
189 | 192 |
|
190 | 193 | export async function exportList(files: string[], workspaceFolder: string, namespace: string): Promise<any> {
|
|
0 commit comments