Skip to content

Commit

Permalink
Update website
Browse files Browse the repository at this point in the history
  • Loading branch information
mbloch committed Apr 16, 2024
1 parent 6331e18 commit 1596b45
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
36 changes: 17 additions & 19 deletions mapshaper-gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2443,33 +2443,31 @@

function WriteFilesProxy(gui) {
// replace CLI version of writeFiles()
internal.replaceWriteFiles(function(files, opts, done) {
internal.replaceWriteFiles(async function(files, opts) {
var filename;
if (!utils$1.isArray(files) || files.length === 0) {
done("Nothing to export");
throw Error("Nothing to export");
} else if (GUI.canSaveToServer() && !opts.save_to_download_folder) {
var paths = internal.getOutputPaths(utils$1.pluck(files, 'filename'), opts);
var data = utils$1.pluck(files, 'content');
saveFilesToServer(paths, data, function(err) {
var msg;
if (err) {
msg = "<b>Direct save failed</b><br>Reason: " + err + ".";
msg += "<br>Saving to download folder instead.";
gui.alert(msg);
// fall back to standard method if saving to server fails
internal.writeFiles(files, {save_to_download_folder: true}, done);
} else {
if (files.length >= 1) {
gui.alert('<b>Saved</b><br>' + paths.join('<br>'));
}
done();
var msg;
try {
await utils$1.promisify(saveFilesToServer)(paths, data);
if (files.length >= 1) {
gui.alert('<b>Saved</b><br>' + paths.join('<br>'));
}
});
} catch(err) {
msg = "<b>Direct save failed</b><br>Reason: " + err.message + ".";
msg += "<br>Saving to download folder instead.";
gui.alert(msg);
// fall back to standard method if saving to server fails
await internal.writeFiles(files, {save_to_download_folder: true});
}
} else if (files.length == 1) {
saveBlobToLocalFile(files[0].filename, new Blob([files[0].content]), done);
await gui.promisify(saveBlobToLocalFile)(files[0].filename, new Blob([files[0].content]));
} else {
filename = internal.getCommonFileBase(utils$1.pluck(files, 'filename')) || "output";
saveZipFile(filename + ".zip", files, done);
await utils$1.promisify(saveZipFile)(filename + ".zip", files);
}
});
}
Expand Down Expand Up @@ -4754,7 +4752,7 @@
if (files.length == 1 && checkboxOn(clipboardCheckbox)) {
await saveFileContentToClipboard(files[0].content);
} else {
await utils$1.promisify(internal.writeFiles)(files, opts);
await internal.writeFiles(files, opts);
}
}

Expand Down
50 changes: 31 additions & 19 deletions mapshaper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11557,7 +11557,6 @@
return ss && ss.isFile() || false;
};


// cli.fileSize = function(path) {
// var ss = cli.statSync(path);
// return ss && ss.size || 0;
Expand Down Expand Up @@ -11600,16 +11599,32 @@
};

// content: Buffer or string
cli.writeFile = function(fname, content, cb) {
var fs = require$1('rw');
cli.writeFileSync = function(fname, content) {
cli.createDirIfNeeded(fname);
if (cb) {
fs.writeFile(fname, content, cb);
if (utils.isString(content)) {
require$1('fs').writeFileSync(fname, content);
} else {
fs.writeFileSync(fname, content);
// as of Node.js v20, on a typical machine, max buffer size is 4gb but the max
// write buffer size is 2gb. An error is thrown when writing >2gb and <4gb.
// To support writing files up to 4gb, files are written in chunks.
cli.writeFileInChunks(fname, content, 1e7);
}
};

cli.writeFileInChunks = function(fname, buffer, chunkSize) {
var fs = require$1('fs');
var fd = fs.openSync(fname, 'w');
var offset = 0;
var bufLen = buffer.length;
var bytesWritten, bytesToWrite;
do {
bytesToWrite = Math.min(chunkSize, bufLen - offset);
bytesWritten = fs.writeSync(fd, buffer, offset, bytesToWrite);
offset += bytesWritten;
} while (bytesWritten > 0 && offset < bufLen);
fs.closeSync(fd);
};

// Returns Node Buffer
cli.convertArrayBuffer = function(buf) {
var src = new Uint8Array(buf),
Expand Down Expand Up @@ -11700,9 +11715,8 @@
return obj;
};

function writeFiles(exports, opts, cb) {
cb = cb || function() {};
return _writeFiles(exports, opts, cb);
async function writeFiles(exports, opts) {
return _writeFiles(exports, opts);
}

// Used by GUI to replace the CLI version of writeFiles()
Expand All @@ -11711,13 +11725,13 @@
_writeFiles = func;
}

var _writeFiles = function(exports, opts, cb) {
var _writeFiles = function(exports, opts) {
if (exports.length > 0 === false) {
message("No files to save");
} else if (opts.dry_run) ; else if (opts.stdout) {
// Pass callback for asynchronous output (synchronous output to stdout can
// trigger EAGAIN error, e.g. when piped to less)
return cli.writeFile('/dev/stdout', exports[0].content, cb);
// Using async writeFile() function -- synchronous output to stdout can
// trigger EAGAIN error, e.g. when piped to less.
require$1('rw').writeFile('/dev/stdout', exports[0].content, function() {});
} else {
if (opts.zip) {
exports = [{
Expand All @@ -11741,11 +11755,10 @@
if (!opts.force && inputFiles.indexOf(path) > -1) {
stop('Need to use the "-o force" option to overwrite input files.');
}
cli.writeFile(path, obj.content);
cli.writeFileSync(path, obj.content);
message("Wrote " + path);
});
}
if (cb) cb(null);
};

function getOutputPaths(files, opts) {
Expand Down Expand Up @@ -23469,8 +23482,7 @@ ${svg}
if (pathInfo.directory) {
o.directory = pathInfo.directory;
// no longer checking for missing directory
// (cli.writeFile() now creates directories that don't exist)
// cli.validateOutputDir(o.directory);
// (cli.writeFileSync() now creates directories that don't exist)
}
if (/gz/i.test(pathInfo.extension)) {
// handle arguments like -o out.json.gz (the preferred format)
Expand Down Expand Up @@ -45388,7 +45400,7 @@ ${svg}
//// catalog = null;
job.catalog = new Catalog();
}
await utils.promisify(writeFiles)(outputFiles, opts);
await writeFiles(outputFiles, opts);

} else if (name == 'point-grid') {
outputLayers = [cmd.pointGrid(targetDataset, opts)];
Expand Down Expand Up @@ -45584,7 +45596,7 @@ ${svg}
});
}

var version = "0.6.89";
var version = "0.6.90";

// Parse command line args into commands and run them
// Function takes an optional Node-style callback. A Promise is returned if no callback is given.
Expand Down

0 comments on commit 1596b45

Please sign in to comment.