001package org.jdrupes.builder.nodejs;
002
003import java.io.File;
004import java.io.IOException;
005import java.io.InputStream;
006import java.net.URL;
007import java.nio.file.Files;
008import java.nio.file.Path;
009import java.nio.file.StandardCopyOption;
010import java.util.zip.ZipEntry;
011import java.util.zip.ZipInputStream;
012
013public class NodeDownloader {
014
015//    private final Path baseDir;
016//
017//    public NodeDownloader(Path baseDir) {
018//        this.baseDir = baseDir;
019//    }
020//
021//    public void ensureInstalled(String version) throws Exception {
022//        Path installDir = getInstallDir(version);
023//        if (Files.exists(installDir)) {
024//            return; // already installed
025//        }
026//
027//        Files.createDirectories(baseDir);
028//
029//        Platform platform = detectPlatform();
030//        String fileName = buildFileName(version, platform);
031//        String downloadUrl
032//            = "https://nodejs.org/dist/v" + version + "/" + fileName;
033//
034//        Path archive = baseDir.resolve(fileName);
035//
036//        download(downloadUrl, archive);
037//
038//        if (fileName.endsWith(".zip")) {
039//            unzip(archive, baseDir);
040//        } else if (fileName.endsWith(".tar.gz")) {
041//            untarGz(archive, baseDir);
042//        }
043//
044//        Files.deleteIfExists(archive);
045//    }
046//
047//    public File getNpmExecutable(String version) {
048//        Platform platform = detectPlatform();
049//        Path installDir = getInstallDir(version);
050//
051//        if (platform.isWindows) {
052//            return installDir.resolve("npm.cmd").toFile();
053//        } else {
054//            return installDir.resolve("bin/npm").toFile();
055//        }
056//    }
057//
058//    private Path getInstallDir(String version) {
059//        Platform p = detectPlatform();
060//        String folderName = "node-v" + version + "-" + p.platformName;
061//        return baseDir.resolve(folderName);
062//    }
063//
064//    private void download(String url, Path target) throws IOException {
065//        try (InputStream in = new URL(url).openStream()) {
066//            Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
067//        }
068//    }
069//
070//    private void unzip(Path zipFile, Path targetDir) throws IOException {
071//        try (ZipInputStream zis
072//            = new ZipInputStream(Files.newInputStream(zipFile))) {
073//            ZipEntry entry;
074//            while ((entry = zis.getNextEntry()) != null) {
075//                Path newPath = targetDir.resolve(entry.getName());
076//                if (entry.isDirectory()) {
077//                    Files.createDirectories(newPath);
078//                } else {
079//                    Files.createDirectories(newPath.getParent());
080//                    Files.copy(zis, newPath,
081//                        StandardCopyOption.REPLACE_EXISTING);
082//                }
083//            }
084//        }
085//    }
086//
087//    private void untarGz(Path tarGz, Path targetDir) throws IOException {
088//        try (InputStream fi = Files.newInputStream(tarGz);
089//                InputStream gzi = new GzipCompressorInputStream(fi);
090//                TarArchiveInputStream tais = new TarArchiveInputStream(gzi)) {
091//
092//            TarArchiveEntry entry;
093//            while ((entry = tais.getNextTarEntry()) != null) {
094//                Path newPath = targetDir.resolve(entry.getName());
095//                if (entry.isDirectory()) {
096//                    Files.createDirectories(newPath);
097//                } else {
098//                    Files.createDirectories(newPath.getParent());
099//                    Files.copy(tais, newPath,
100//                        StandardCopyOption.REPLACE_EXISTING);
101//                }
102//            }
103//        }
104//    }
105//
106//    private Platform detectPlatform() {
107//        String os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
108//        String arch
109//            = System.getProperty("os.arch").contains("64") ? "x64" : "x86";
110//
111//        if (os.contains("win")) {
112//            return new Platform("win-" + arch, true, ".zip");
113//        } else if (os.contains("mac")) {
114//            return new Platform("darwin-" + arch, false, ".tar.gz");
115//        } else if (os.contains("nux")) {
116//            return new Platform("linux-" + arch, false, ".tar.gz");
117//        }
118//
119//        throw new RuntimeException("Unsupported OS: " + os);
120//    }
121//
122//    private String buildFileName(String version, Platform p) {
123//        return "node-v" + version + "-" + p.platformName + p.extension;
124//    }
125//
126//    private static class Platform {
127//        final String platformName;
128//        final boolean isWindows;
129//        final String extension;
130//
131//        Platform(String platformName, boolean isWindows, String extension) {
132//            this.platformName = platformName;
133//            this.isWindows = isWindows;
134//            this.extension = extension;
135//        }
136//    }
137//
138//    }
139//
140//    public NpmDownloader() {
141//        // TODO Auto-generated constructor stub
142//    }
143}